迈出尝试一起使用所有这些技术的第一步..我遇到了一些麻烦..
这是我的服务器端:
[WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string simplestMethod() { return "Simplest method returned"; }
这是我的客户端:
$(document).ready(function(){ $("a").click(function(event){ $.ajax({ type: "POST", url: "http://localhost:53346/d2/TAPI.asmx/simplestMethod", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error Occured!" +" | " + XMLHttpRequest +" | " + textStatus +" | " + errorThrown ); } }); }); });
结果是一条警告:
发生了错误! | [对象 XMLHttpRequest] |解析错误 |不明确的。
什么解析失败以及为什么?
我应该提到直接调用 WS 方法确实有效。
非常感谢!
最佳答案
您的代码看起来不错,有一个可疑的地方:url
。您应该将 url
替换为 "TAPI.asmx/simplestMethod"
或 "/d2/TAPI.asmx/simplestMethod"
。
此外,如果您想研究如何使用参数调用 Web 方法或从 Web 方法返回更复杂的数据,请参阅 How do I build a JSON object to send to an AJAX WebService?和 asmx web service, json, javascript/jquery? , Can I return JSON from an .asmx Web Service if the ContentType is not JSON? 。如何从 Web 方法内部的异常中解码错误消息,请参阅 Get xhr object in vb.net while ajax calling fails .
关于.net - 使用 JSON 从 AJAX 和 JQuery 调用简单的 Web 服务(.asmx 文件) - 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3650561/