0

I am using jquery to do ajax calls:

    // omitting the code for the options properties

    var options = {
        type: Type,
        url: Url,
        data: '{aString:"abc"}',
        contentType: ContentType,
        dataType: dataType,
        //processdata: ProcessData,
        success: function (msg) {
            ServiceSucceeded(msg);
        },
        error: ServiceFailed

    };
    function ServiceFailed(result) {
        alert('Service call failed: ' + result.status + '' + result.statusText);
    }

    $.ajax(options).done(function () {
      alert("success: " + msg);
    });

This call works in that the url defined in options is called. The endpoint is a wcf service which I host so I have verified it's called as expected.

I monitor the call with fiddler, and I see nothing wrong with the request or the response. The http response code is 200 OK.

But the function in done is not called. Instead ServiceFailed is run. Why is this? Why is done() not called, and why does jquery consi

Manolo
  • 1,597
  • 4
  • 21
  • 35

3 Answers3

3

We can only guess on the basis of what you've posted.

You're specifying a dataType to jQuery (but haven't told us what that data type is), which means you're (potentially) telling it to transform the result. For instance, if your dataType variable is "json", jQuery will try to convert the result to JSON; if it's "xml", jQuery will try to convert the result to XML.

If you're watching the call occur and seeing a 200 response with content, that suggests to me that it's the data conversion that fails.

You can readily find out more about why an error occurs. The error function is called with this signature:

function error(jqXHR, textStatus, errorThrown)

...so you can put a breakpoint inside it and inspect textStatus (which should be 200 based on your monitoring of the result, but if it isn't that's useful information) and the errorThrown, which would probably give you some idea what went wrong.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    You are right, I just saw that there were more arguments in the error function. the second argument gave me the answer, an error parsing the results as json. Which is my fault. Thanks! (will mark your response as answer as soon as stackoverflow allows me to. – Manolo Nov 26 '12 at 09:03
0

If your Url is remote then you will need to use JSONP, which requires you to add a querystring to the Url ?callback?

jQuery ajax working on local file but not working from a remote url?

Community
  • 1
  • 1
ThatGuyInIT
  • 2,239
  • 17
  • 20
0

u havn't specified the dataType....as to what to expect in the response from the server..... jQuery processes the response data and passes it as the first argument to your success callback function (if provided). You may specify the dataType as a key/value pair in the lone argument passed to $.ajax. Supported types are:

"xml": Treat the response as an XML document that can be processed via jQuery.
"html": Treat the response as HTML (plain text); included script tags are evaluated.
"script": Evaluates the response as JavaScript and evaluates it.
"json": Evaluates the response as JSON and sends a JavaScript Object to the success callback.

check out this page

http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

bipen
  • 36,319
  • 9
  • 49
  • 62