Manolo
Manolo

Reputation: 1676

jquery ajax error when nothing wrong with http request

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

Upvotes: 0

Views: 677

Answers (3)

bipen
bipen

Reputation: 36531

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

Upvotes: 0

ThatGuyInIT
ThatGuyInIT

Reputation: 2239

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?

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1073968

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.

Upvotes: 3

Related Questions