我有以下服务:
[WebService(Namespace = "http://tono.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
我可以通过执行以下操作从html调用HelloWorld方法:
<form action="http://localhost:10144/Services/WebService1.asmx/HelloWorld" method="POST">
<input type="submit" value="Invoke" class="button">
</form>
为什么我无法使用ajax调用它?为什么javascript代码会失败?
<script type="text/javascript">
function CallService() {
$.ajax({
type: "POST",
utl: "http://localhost:10144/Services/WebService1.asmx/HelloWorld",
//data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
debugger;
var test1 = response.d;
var test2 = response.data;
},
error: function (er) {
debugger;
}
});
}
</script>
答案 0 :(得分:1)
如上所述,你的网址应该是网址。
同样在您的错误功能
中error: function (er) {
debugger;
}
将其更改为以下内容,以便向您显示错误,您可以更好地进行调试。
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
}