【发布时间】:2014-04-15 13:25:04
【问题描述】:
我正在使用 jquery ajax 调用我的网络服务:
$.ajax({
type: sType,
url: "myWebService.asmx",
cache: serviceCash,
data: params,
accepts: {
text: "application/json"
},
contentType: contentType,
dataType: "json"
....
Web 服务如下所示:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetLang(string cLang, string iLangVersion ){
//code to create json string
return jsonResp;
}
当我输入:POST evertyhing 完美无缺。
type: "POST",
data: "{ 'clang':'ANG', 'iLangVersion' : '1'}",
contentType:"application/json; charset=UTF-8",
dataType: "json"
响应是纯json:
{"d":"{\"userSettingsData\":{\" ...and so on
当我将请求更改为 GET 方法时:
type: "GET",
data: "clang=ANG&iLangVersion=1",
contentType:"application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json"
响应是xml:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"translation":"\r\n{\"translation\":{\"..and so on
我应该改变什么才能像以前一样以 json 格式获得响应? 因此,我将 url 编码的数据发送到 Web 服务,作为回报,我想获取 json 而不是 XML! 我已经定义了 dataType: "json" - 你期望从服务器返回的数据类型 我设置了接受类型:接受:{text: "application/json"} - 告诉服务器它将接受什么样的响应作为回报。
但我得到了 xml 作为回报。我应该改变什么?
【问题讨论】:
-
这一切都取决于服务器端的 API。详情请联系开发者。