你有方法名称追加到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
,大概要use the GET
request:
Setting cache to false will only work correctly with HEAD and GET requests.
It works by appending "_={timestamp}" to the GET parameters.
禁用布局? – iJade
使用这个:http://stackoverflow.com/questions/4164114/posting-json-data-to-asp-net-mvc 看看这家伙是如何使用返回的Json模型。 –