很多时候我们在建立Asp.net的Web服务的时候会采用WebService来建立,一般情况下其实是没有什么问题的,但是如果将这个服务直接去用前端的Ajax去调用的时候便会有一些问题。
一般情况下我们会用如下的代码去调用我们的WebService:
function callSvc() { $.ajax({ type: "POST", url: "Service1.asmx/HelloWorld", data: "", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data, status) { alert(data); }, error: function(request, status, error) { alert(request); alert(status); alert(error); } }); }
这个时候会报错!
报错信息应该差不多和下面的语句一样:
“Only Web services with a [ScriptService] attribute on the class definition can be called from script.”
这种情况下,我们要稍微修改下我们的WebService:
[System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService
这样就差不多啦。下面提供两个链接快速的用来测试下支不支持Js。
https://<URL>/SimpleWebService.asmx/js
https:///SimpleWebService.asmx/jsdebug