2

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
notnot
  • 4,472
  • 12
  • 46
  • 57
  • So when you put "application/json" as content type are you recieving data in backend as "JSON" ? – insomiac Aug 27 '12 at 23:08
  • I'd assume so - it' supposed to be automatically deserialized by .NET – notnot Aug 27 '12 at 23:18
  • Also make sure you are returning json data from backend.. – insomiac Aug 27 '12 at 23:19
  • So from the backend the data which you return should be in JSON. As you specified the datatype as "json". Frontend is going to expect the data as json, and if it gets something else it will give you error.. – insomiac Aug 27 '12 at 23:24
  • The conversion is supposed to be automatic. I'm trying to figure out why this automatic conversion isn't kicking in. – notnot Aug 27 '12 at 23:26
  • Nothing to do with the JSON bit, Use Google Chrome or any Javascript debugger to see what is returned, and if any errors came back to the consol – ShaunOReilly Aug 28 '12 at 03:58

2 Answers2

2

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.

Community
  • 1
  • 1
notnot
  • 4,472
  • 12
  • 46
  • 57
0

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

ShaunOReilly
  • 2,186
  • 22
  • 34