1
votes

I have an .Ajax method that calls a method on the page behind (does some logic, then calls a WCF service, then returns an object) that looks like this:

$.ajax({
            type: 'POST',
            url: 'MyPage.aspx/TestMethod',
            dataType: "json",
            contentType: "application/json",
            data: "{'Id': '" + Id + "'}",
            success: function (data) {
                $("#randomElement").html(data.d.Foo);
            },
            error: function ((xhr, textStatus, error){
            alert(xhr.statusText);
            alert(textStatus);
            alert(error);

        });

And the page behind:

  [System.Web.Services.WebMethod]
        public static MyObject TestMethod(string Id)
        {
            //Logic removed for readability!

            return MyService.GetStuff(Id);

        }

This works perfectly fine on my local machine, but fails to work when I deploy to my server.

I have determined that the page behind method does not get called (i.e. the error is in calling TestMethod(), not TestMethod() calling the service).

I have also placed an alert before the .ajax method to show me the path of the page I am on just in case it was doing something funny with the uri on the live server, and it comes back as "/MyPage.aspx" as I would expect.

I also changed the "url:" to the full uri to see if that helped and it didn't.


After running fiddler with custom errors off, I get the following error:

{"Message":"Thread was being aborted.","StackTrace":" at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)\r\n at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)\r\n at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)\r\n at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.Threading.ThreadAbortException"}

3
Should the url be MyPage.asmx/TestMethod instead of MyPage.aspx/TestMethod?John Pick
What response are you getting from the ajax request when it fails on the server? Is it returning a 200, 404, 500, something else?kendaleiv
@JohnPick No, the call is to an aspx web page behind method, not an asmx web service.Ben
@kendaleiv I have updated the code in the question to show the error handling I have put in place (found from the internet). The messages that show in the 3 alerts are: "Error", "Error" and "Internal Server Error" in that order. Is there a way to get better error details?Ben
You can examine what the server is returning for the ajax request with Firebug, Chrome Developer Tools, or Fiddler.kendaleiv

3 Answers

1
votes

Your JSON is not valid; you need to use double quotes:

data: '{"Id": "' + Id + '"}',
0
votes

To me it seems like the it is not posting the right parameters to your method.

I've never used the data option in the .ajax method like you do here before. I use:

data: {Id: myId},

Where myId is the variable holding the value of the Id you want to pass - without any quotes.

the \r\n is usually something in relation to encoding strings which is not something that should be relevant here, and it seems like you are passing a string data: "{'Id': '" + Id + "'}" instead of an object.

0
votes

Does your server has a "json"? godaddy, for example, does not offer it