我正在尝试使用jQuery通过GET方法调用Web服务功能,但遇到了问题.这是一个Web服务代码:
[WebService(Namespace = "http://something.com/samples")] [ScriptService] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class TestWebService : System.Web.Services.WebService { [WebMethod] public string Test2() { string result = null; try { result = "{'result':'success','datetime':'" + DateTime.Now.ToString() + "'"; } catch (Exception ex) { result = "Something wrong happened"; } return result; } }
$.ajax({ type: "GET",url: "http://localhost/testwebsite/TestWebService.asmx/Test2",data: "{}",contentType: "application/json",dataType: "json",error: function (xhr,status,error) { alert(xhr.responseText); },success: function (msg) { alert('Call was successful!'); } });
<string> {'result':'success','datetime':'4/26/2010 12:11:18 PM' </string>
解决方法
Enable ASP.NET ASMX web service for HTTP POST / GET requests
[WebMethod] [ScriptMethod(UseHttpGet = true)] public string Test2() { [...] }