目前我正在做一个asp。net网站,我试图从Web服务获取数据,并将检索到的数据显示到表中。下面是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>
对于Web服务
[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;
}
一直得到这个错误,当我检查元素,但仍然没有得到错误的地方。我点击了http://localhost:2179/CaregiverService.asmx/createJsDataTable
从检查元素在谷歌chrome和它的工作正常。希望有人能帮我解决它: x
我不知道这是否能解决您的问题,但我非常确定Asp。默认情况下,net Web服务仅从POST方法获取。因此,尝试将类型更改为POST。
或
设置尝试在Web服务的接收端设置此设置
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
或
在你的网站上。试试这个
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>