Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajax() error in jquery-1.6.2 but not in jquery-1.4.1

I used $.ajax() to consume a local .asmx webservice. Here's my code for the call:

    $("#btnGetOne").click(function() {
        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: 'http://localhost:53003/TestWebService.asmx/GetServant',
            data: '{ "servant_id": "' + $("#txtServantID").val() + '" }',
            dataType: 'json',
            success: function(data, textStatus, jqXHR) {
                var jsnData = $.parseJSON(data.d);
                $('#DisplayArea').html(jsnData.Servant_Name);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert(textStatus + ' ' + errorThrown);
            }
        });
    });

As you can see the ajax call executes when I click btnGetOne.

As in my question header, this works in jquery-1.4.1, but when I used jquery-1.6.2 I get an errorThrown saying No Transport.

Is there anything else I'm missing?

like image 862
Erick Garcia Avatar asked Jul 26 '11 10:07

Erick Garcia


People also ask

What causes Ajax errors?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.

What is Ajax failure?

The AJAX fail is a global event that triggered on the document to call handler function, which may be listening. The ajax fail can be performed with the help of the ajaxError() function. The jQuery ajaxError() function is a built-in function in jQuery.

What is success and error in Ajax?

jquery ajax calls are asynchronous by default. So success & error functions will be called when the ajax load is complete. But your return statement will be executed just after the ajax call is started.


1 Answers

Your HTML+JS page is probably not loaded from localhost:53003, while you are trying to, using Ajax, an URL that is on domain localhost:53003.

So, it looks like you are trying to make a cross-domain request, which is forbidden -- see Same Origin Policy.


Taking a look a the documentation of jQuery.support, you might want to enable jQuery.support.cors (quoting) :

cors is equal to true if a browser can create an XMLHttpRequest object and if that XMLHttpRequest object has a withCredentials property.

To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests (windows gadget, etc), set $.support.cors = true;.


And here are a couple of links that might help :

  • jQuery Call to WebService returns “No Transport” error
  • Force jQuery 1.5 to always allow cross-site scripting
  • BB5 - Fix for jQuery.ajax "no transport" error
like image 170
Pascal MARTIN Avatar answered Oct 05 '22 11:10

Pascal MARTIN