2012-08-27 103 views
2

從我能收集的信息來看,問題在於PageMethod沒有返回JSON。我是否必須在服務器端執行其他操作才能正確格式化返回值?還有什麼我失蹤?對於PageMethod的ASP.NET jQuery AJAX調用返回帶有200個響應的parsererror

(注:我是IE8現在測試這個)(使用jQuery 1.8.0)

在客戶端:

$.ajax({ 
      type: "POST", 
      url: "Test.aspx/GetDate", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: SuccessFunction, 
      error: ErrorFunction 
     }); 

在服務器端:

<WebMethod()> _ 
Public Shared Function GetDate() As String 
    Return Date.Now.ToString 
End Function 
+0

刪除的contentType。 – insomiac

+0

產生相同的結果。 – notnot

+0

所以當你把「application/json」作爲內容類型的時候,你在後端接收數據爲「JSON」? – insomiac

回答

2

好的,所以我根據this這個老問題想出了這個問題。原來我需要在我的web.config文件system.web部分如下:

<httpModules> 
    <add name="ScriptModule" 
    type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
</httpModules> 

我想這是,如果你創建一個「AJAX網頁」與Visual Studio將自動爲您設置,但我試圖添加一些東西到一箇舊的ASP.NET頁面。

0

以下爲我工作:

function GetShoppingCartData() { 
    jQuery.ajax({ 
     type: "POST", 
     url: "DesktopModules/EcomDnnProducts/AjaxProductDisplay.aspx/GetShoppingCartData", 
     data: "{'CartId': '" + jQuery(".shoppingcartid").attr("value") + "'}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async: true, 
     cache: false, 
     success: function (msg) { 
      //jQuery('#productattributesdata').text(msg.d); 
      buildShoppingCart(msg.d); 
     }, 
     fail: function (msg) { 
      jQuery('#productattributesdata').text(msg.d); 
     } 
    }); 
} 

你並不需要的「數據顯示:......」有點

我不得不更改我的ASP頁面,得到它的工作。 我的函數如下:

<System.Web.Services.WebMethod()> _ 
    Public Shared Function GetShoppingCartData(ByVal CartId As String) As String 
     Dim ReturnString As String = "" 

     Try 

      ReturnString = "test1;test2;test3" 

     Catch ex As Exception 
      'ProcessModuleLoadException(Me, exc) 
      Dim DataLogger As New DataLogger 
      DataLogger.WriteToEventLog("Error", ex.Message & " - " & ex.StackTrace) 
     End Try 

     Return ReturnString 
    End Function 

會看看是否有任何其他設置...

添加以下到Web.config授予權限的事情被稱爲:

<httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     </customHeaders> 
    </httpProtocol> 
    </system.webServer> 

不知道這是否是缺失的位。

一些更多的資源: http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.80).aspx

http://www.dotnetcurry.com/ShowArticle.aspx?ID=109

http://forums.asp.net/t/1298480.aspx/1

HTH 肖恩