[英]jquery ajax call to asp.net webservice
我正在嘗試從Jquery ajax調用webservice方法。
這是我的jQuery代碼,
function abc() {
alert("");
$.ajax({
type: "Get",
url: "abc.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (errorThrown) {
console.log(errorThrown.d);
alert(errorThrown + "what's wrong?");
},
success: function (msg) {
console.log(msg.d);
alert(msg.d);
return false;
// Do something interesting here.
}
});
return false; }
而我的網絡方法如下
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
jQuery錯誤事件不斷發生。 不知道為什么會這樣
任何幫助將不勝感激
謝謝
我在WebService
類上缺少[ScriptService]
屬性,類似於以下內容:
[ScriptService]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}
現在,它運行良好。
嘗試此操作,因為您期望服務器提供JSON結果,並添加ScriptMethod
將使javascript調用您的方法
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
還要檢查如何讓ASMX文件輸出JSON
我猜您需要指定該方法將響應哪種請求。
像這樣的東西
[HttpGet]
public string HelloWorld() {
return "Hello World";
}
Or use the convention
public string Get() {
return "Hello World";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.