【发布时间】:2021-03-31 17:04:03
【问题描述】:
我正在尝试使用 ajax 调用从 Web 服务中获取数据,其函数的返回值为 json 格式的字符串。
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyClass : WebService {
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public Chart RetrieveData(string countries, string startDate, string endDate)
{
Chart c = getData(countries, startDate, endDate);
return c;
}
}
我正在使用 Ajax,如下所示:
var path = 'pathToXml/file.asmx/RetrieveData?countries=' + countries + '&startDate=' + startDate + '&endDate=' + endDate;
$.ajax({
type: "GET",
url: path,
contentType: 'application/json; charset=utf-8',
success: function (data) {
console.log(data.d);
},
error: function (textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
}
});
【问题讨论】:
-
您需要访问
data.d或msg.d值...stackoverflow.com/a/36822653/4000335 -
@kshkarin 对于我的第一个 ajax 调用,无论我是否将数据类型指定为“文本”,data.d 都会给我“欠精细”。而我的第二个 ajax 只是有错误,所以我认为这不会起作用。