您必须将方法名称附加到 URL(/HelloWorld
例如)并method: "post"
在您的 ajax 调用中指定(如果您想使用POST
请求)。尝试这个:
$.ajax({
url: "http://10.0.2.2/SampleService/Service/HelloWorld.asmx/HelloWorld",
method:"POST",
dataType: "text",
cache: false,
crossDomain: true,
timeout: 15000,
success: function (rtndata) {
alert('Got Success ::' + rtndata);
},
error: function (xhr, errorType, exception) {
alert("Excep:: " + exception + "Status:: " + xhr.statusText);
}
});
如果您想GET
用作请求方法,请确保您的web.config文件中有此标记:
<system.web>
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
此外,您需要使用以下方式装饰服务方法ScriptMethodAttribute
:
[ScriptMethod(UseHttpGet = true)]
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
ajax 调用 (method: "GET"
是可选的,因为它是 ) 的默认方法text
:
$.ajax({
url: "http://localhost:57315/helloworld.asmx/HelloWorld",
method: "GET"
dataType: "text",
cache: false,
crossDomain: true,
timeout: 15000,
success: function (rtndata) {
alert('Got Success ::' + rtndata);
},
error: function (xhr, errorType, exception) {
alert("Excep:: " + exception + "Status:: " + xhr.statusText);
}
});
当您使用cache: false
时,可能您想使用GET
请求:
Setting cache to false will only work correctly with HEAD and GET requests.
It works by appending "_={timestamp}" to the GET parameters.