2009-05-22 198 views
8

我可以使用jQuery調用我的webservice如果contentType =「application/x-www-form-urlencoded; charset = utf-8 「ASMX webservice不返回JSON,只能使用應用程序POST/x-www-form-urlencoded contentType

這將然而,返回XML:<string>[myjson]</string>

如果嘗試使用張貼到服務‘應用程序/ JSON;字符集= UTF-8’我接收500錯誤與空棧跟蹤和ExceptionType 。我的web服務功能從來沒有被打過,所以我不太清楚如何調試這種情況。

我的方法和類裝飾有適當的屬性,並設置爲使用JSON作爲它們的響應類型(我的wsdl和disco文件也是如此)。我已經安裝了Ajax擴展和web.config中的必需條目。

這是在SharePoint服務器場上,但我不確定這會帶來太多的不同。我在所有WFE上部署了web.config更改,並安裝了ajax擴展。該服務再次運行,它只是不接受任何內容,而是默認的內容類型。

不知道我很想念這裏,夥計們......

我的Ajax調用:

$.ajax({ 
type: "POST", 
url: "/_vti_bin/calendar.asmx/Test", 
dataType: "json", 
data: "{}", 
contentType: "application/json; charset=UTF-8", 
success: function(msg){ 
    alert(msg); 
    }, 
error: function(xhr, msg){ alert(msg + '\n' + xhr.responseText); } 
}); 

我的web服務類:

[WebService(Namespace = "http://namespace")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[ScriptService()] 
public class CalendarService : WebService 
{ 
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string Test() 
    { 
     return "Hello World"; 
    } 
} 
+0

執行AJAX請求時,ASP.NET MVC 1.0上發生的情況也是如此。 – 2010-09-24 15:25:44

回答

0

不知道它可能是這個簡單,但我使用jQuery從我的web方法回調JSON。

我看到的主要區別是類的屬性

[System.Web.Script.Services.ScriptService]

[WebService(Namespace = "http://MyNameSpace")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.Web.Script.Services.ScriptService] 
    public class Web : System.Web.Services.WebService{ 

     [WebMethod] 
     public string TestMethod(){ 
     return "Testing"; 
     } 

    } 

我中有你使用的是3.5的假設因爲這是公開JSON Web方法的唯一方法。

我的調用窗體jQuery的外觀幾乎完全相同,所以沒有問題。

+0

嘗試了這一點,仍然得到XML包裝... – 2009-05-27 17:14:40

-1

看起來您必須在scriptMethod標記中指定json作爲響應格式。這是vb.net,但我敢肯定你的想法:

ResponseFormat:= ResponseFormat.Json

+0

您可以在粘貼的代碼中看到,我將響應格式提供給ScriptMethod屬性。 – 2009-05-27 17:14:09

0

如果你在IE測試此,請嘗試從您的contentType屬性去掉字符集聲明(也就是說,它應該是這樣的:

contentType: "application/json", 

我還沒有發現爲什麼,但IE瀏覽器似乎得到它的短褲在扭曲時,用「charset=UTF-8」部分進行JSON調用。

alt text

0

我認爲你正在尋找的WebInvoke或WebGet屬性,它可以讓你指定URI模板,機身風格,請求和responseformats,例如:

[WebGet(ResponseFormat= WebMessageFormat.Json)] 

This鏈接可以幫助。還有一個類似的WebInvoke文章(主要用於發佈)。

+1

這個問題是關於ASMX web服務的; WebGet適用於WCF合約。 – 2009-07-22 19:39:28

0

我使用JQuery AJAX JSON調用ASMX webservice相當多。它適用於所有瀏覽器。我使用安裝了ASP.NET AJAX Extensions的.NET 2.0(捆綁在3.5中)。

我的班級和你的裝飾器一樣。我的方法只有[WebMethod(EnableSession = true)]修飾符。但是我的web.config在其HttpHandlers的部分如下條目:

<add verb="*" path="*jsonservice.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 

我的jQuery的調用看起來如下:

$.ajax({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: "path/to/service/jsonservice.asmx/MethodName", 
    data: JSON.stringify(DTO), 
    dataType: "json", 
    success: function(rtrn) { alert("good"); }, 
    error: function(req, msg, err) { alert("bad"); } 
}); 

This article是我認識的根源。

3

我在2.0中使用web服務工作,但我已經放置了對.d(請參閱下面的dataFilter)的保護。我也正在返回一組對象。注意:對象的類是靜態的,否則至少對我來說不能正常工作。

$.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     data: "{}", 
     dataFilter: function(data) 
     { 
      var msg; 
      if (typeof (JSON) !== 'undefined' && 
       typeof (JSON.parse) === 'function') 
       msg = JSON.parse(data); 
      else 
       msg = eval('(' + data + ')'); 
      if (msg.hasOwnProperty('d')) 
       return msg.d; 
      else 
       return msg; 
     }, 
     url: "webservice/ModifierCodesService.asmx/GetModifierList", 
     success: function(msg) 
     { 
      LoadModifiers(msg); 
     }, 
     failure: function(msg) 
     { 
      $("#Result").text("Modifiers did not load"); 
     } 
    }); 

這裏是我的web服務的snippett:

...

[WebService(Namespace = "http://mynamespace.com/ModifierCodesService/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class ModifierCodesService : System.Web.Services.WebService 
{ 

    /// <summary> 
    /// Get a list of Modifiers 
    /// </summary> 
    /// <returns></returns> 
    [WebMethod(EnableSession = true)] 
    public Modifier[] GetModifierList() 
    { 
     return GetModifiers(); 
    } 
     /// <summary> 
     /// Modifiers list from database 
     /// </summary> 
     /// <returns></returns> 
     private Modifier[] GetModifiers() 
     { 
      List<Modifier> modifier = new List<Modifier>(); 
      ModifierCollection matchingModifiers = ModifierList.GetModifierList(); 
      foreach (Modifier ModifierRow in matchingModifiers) 
      { 
       modifier.Add(new Modifier(ModifierRow.ModifierCode, ModifierRow.Description)); 
      } 
      return modifier.ToArray(); 
     } 
    } 

...

目標代碼:

public static class ModifierList 
    { 

     /// <summary> 
     /// Returns the Modifier Collection. 
     /// </summary> 
     /// <param name="prefix"></param> 
     /// <returns></returns> 
     public static ModifierCollection GetModifierList() 
     { 
+1

SO底線更改 public string Test(){ 至 public static string Test(){ 並查看它是否適用於您。 – 2009-09-29 19:47:04

1

我有一直在與今天的戰鬥一個iPhone應用程序與.Net Web服務交談。

我發現,如果我將內容類型更改爲application/jsonrequest,它會經歷一個沒有問題的情況,並且我能夠處理我的Web服務器中的數據。

只是爲了笑話,我將上面提到的行添加到了我的web.config中,但它並沒有使application/json工作。

相關問題