Search code examples
jquery.netajaxjsonparse-error

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

Solution

  • 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.