目前我正在做一个asp.net网站,我试图从网络服务获取数据并显示检索到的数据。下面是ajax。
<script type="text/javascript">
$.ajax({
type: "GET",
url: "/CaregiverService.asmx/createJsDataTable",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
console.log(json);
console.log(json.d.aaData);
$('#nursingHomeTable').dataTable({
"aaData": json.d.aaData,
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
</script>
对于网络服务
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public JsonDataTable createJsDataTable()
{
JsonDataTable jsDT = new JsonDataTable();
List<object> vl = new List<object>();
vl.Add("value 1");
vl.Add("value 2");
vl.Add("value 3");
vl.Add("value 4");
jsDT.add_Row(vl);
return jsDT;
}
当我检查元素但是仍然没有得到错误时,我一直在收到错误。我点击了谷歌浏览器中的inspect元素中的http://localhost:2179/CaregiverService.asmx/createJsDataTable
,它运行正常。希望有人能帮助我解决它:x
答案 0 :(得分:0)
我不知道这是否能解决您的问题,但我非常确定Asp.net webservices默认只使用POST方法。所以尝试将类型更改为POST。
OR
设置尝试在网络服务的接收端设置此项
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
OR
你的web.config中的试试这个
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>