我使用协议jsonp来调用web方法。无法使用jquery json调用webmethod
我使用此代码为WebService:
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public string HelloWorld()
{
return "Hello World";
}
}
这jQuery的与Jason在客户端:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'http://localhost:50837/Service1.asmx/HelloWorld',
data: {},
dataType: "json",
success: function(Msg) {
alert('success:' + Msg.d.FirstName);
},
error: function(xhr, textStatus, errorThrown) {
alert("error");
}
});
}
这个jQuery让我总是一个错误信息,但我不知道原因。 有人可以帮助我吗?
对于初学者,将'alert(“error”)''改为'alert(textStatus +“ - ”+ errorThrown):) – 2010-09-09 19:31:51
错误是什么? – epitka 2010-09-09 19:32:22
为什么使用像'http:// localhost:50837/Service1.asmx/HelloWorld'这样的URL而不是'/Service1.asmx/HelloWorld'?如果其他网站上的Web服务和JavaScript程序?你在当前代码中使用jsonp的地方? – Oleg 2010-09-09 19:32:31