2010-04-26 30 views
5

我試圖通過使用jQuery的GET方法調用Web服務函數,但遇到問題。這是一個Web服務代碼:通過使用jQuery的GET方法調用ASP.NET Web服務函數

[WebService(Namespace = "http://something.com/samples")] 
[ScriptService] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class TestWebService : System.Web.Services.WebService { 
    [WebMethod] 
    public string Test2() 
    { 
     string result = null; 

    try 
     { 
      result = "{'result':'success', 'datetime':'" + DateTime.Now.ToString() + "'"; 
     } 
     catch (Exception ex) 
     { 
      result = "Something wrong happened"; 
     } 

     return result; 
    } 

} 

這就是我所說的功能方式:

$.ajax({ type: "GET", 
     url: "http://localhost/testwebsite/TestWebService.asmx/Test2", 
     data: "{}", 
     contentType: "application/json", 
     dataType: "json", 
     error: function (xhr, status, error) { 
      alert(xhr.responseText); 
     }, 
     success: function (msg) { 
      alert('Call was successful!'); 
     } 
    }); 

方法調用成功,但結果字符串被通過XML標籤覆蓋,這樣的:

<string> 
{'result':'success', 'datetime':'4/26/2010 12:11:18 PM' 
</string> 

而我得到一個錯誤,因爲這(錯誤處理程序被稱爲)。有人知道可以做些什麼嗎?

+0

你的'}'在返回的JSON字符串的末尾? – 2010-04-27 13:08:21

+0

你的意思是由XML標籤覆蓋的json字符串?當我從瀏覽器窗口複製它時,我認爲我做錯了什麼,當然它應該在那裏。 – 2010-04-27 16:00:55

+0

也許,但我沒有在Web服務代碼中看到它。 – 2010-04-27 17:56:22

回答

0
  1. 規則JSON: 您只能從同一域訪問數據!

  2. 唯一的例外是當使用jsonp(由於在.NET框架中沒有jsonp序列化程序,實現相當複雜)。
    如果您使用的是標準的Web服務(和不WCF),你可以找到指導HOWTO實現這個here

+0

現在它應該足以讓我訪問數據在同一個域中。但即使我在本地機器的瀏覽器中打開了像這樣的 URL:http://localhost/testwebsite/TestWebService.asmx/Test2 ,我看到了XML呈現。我現在唯一需要的是擺脫XML – 2010-04-26 20:10:06

0

確保它添加到你的Ajax選項:

contentType: "application/json; charset=utf-8" 

你的總體要求應該是這樣得到回JSON,而不是XML:

$.ajax({ type: "GET", 
    url: "http://localhost/testwebsite/TestWebService.asmx/Test2", 
    data: "{}", 
    contentType: "application/json", 
    dataType: "json", 
    contentType: "application/json; charset=utf-8". 
    error: function (xhr, status, error) { 
     alert(xhr.responseText); 
    }, 
    success: function (msg) { 
     alert('Call was successful!'); 
    } 
}); 

ScottGu has a full breakdown on what's required here,但它看起來像你的案例中缺失的contentType(那個人也讓我瘋狂了一段時間)。

+0

更改contentType不會產生任何影響 – 2010-04-26 20:12:43

+0

@the_V - 它提示什麼錯誤?也只是踢,你有同樣的行爲與POST? – 2010-04-26 20:22:04

+0

POST工作正常,但我想讓它與GET一起工作。關於錯誤:xhr.responseText如下 - {'result':'success','datetime':'27.04.2010 0:57:30' 2010-04-26 20:58:38

0

您可以嘗試在您的方法上設置ResponseFormat。看到http://williamsportwebdeveloper.com/cgi/wp/?p=494看看他們是如何做到這一點的JSON。它可能只是默認爲XML。

+0

我試着以下屬性添加到方法的定義: [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = TRUE,XmlSerializeString = FALSE)] 但它沒不工作 – 2010-04-26 20:06:56

0

您需要裝飾與ScriptMethodAttribute方法:

[WebService(Namespace = "http://something.com/samples")] 
[ScriptService] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class TestWebService : System.Web.Services.WebService { 

    [WebMethod] 
    [ScriptMethod] 
    public string Test2() 
    { 
    [...] 
    } 
} 

這將確保該方法在默認情況下(的ResponseFormat默認值爲Json)返回JSON。

+0

[ScriptMethod]屬性不起任何作用 – 2010-04-26 21:04:14

0

你嘗試WebInvokeAttribute,它具有定義請求&響應格式,在這裏你可以設置爲WebMessageFormat.Json成員。
喜歡的東西:
[WebInvoke(UriTemplate = "ServiceName", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]

0
  1. 你可以用它代替 Web服務HTTP處理程序。
  2. 您可以在客戶端上使用 javascript解析xml響應。