notnot
notnot

Reputation: 4642

ASP.NET jQuery AJAX call to PageMethod returns parsererror with 200 response

From what I can gather, the issue is that the PageMethod is not returning JSON. Do I have to do something else on the server side to properly format the return value? Is there something else I'm missing?

(Note: I'm testing this for IE8 right now)

On the client side (using jQuery 1.8.0):

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

On the server side:

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

Upvotes: 2

Views: 2877

Answers (2)

notnot
notnot

Reputation: 4642

OK, so I figured this out based on this older question. Turns out I needed the following in the system.web section of my web.config file:

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

I guess this is set up for you automatically if you create an "AJAX web page" with Visual Studio, but I was trying to add something to an older ASP.NET page.

Upvotes: 2

ShaunOReilly
ShaunOReilly

Reputation: 2196

The following worked for me:

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);
        }
    });
}

you do not need the "data:...." bit

I had to make changes to my ASP page to get it to work. My function looks like this:

   <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

Will see if there was any other settings...

Added the following to the web.config to give permissions for the thing to be called:

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

Not sure if this is the missing bit.

Some more resources: 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 Shaun

Upvotes: 0

Related Questions