我使用協議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