6

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?

Erick Garcia
  • 832
  • 2
  • 12
  • 29
  • 1
    I don't think it's the actual problem, but something doesn't look right in your `success` handler: You've told jQuery that the response will be JSON, and so it will deserialize it for you, but then you're deserializing `data.d`. Unless your response data really defines an object with a property called `d` whose value is a string which *contains* further JSON data (which would be a bit odd, why embed JSON inside a string inside a response that's already in JSON?), I don't think you want that `parseJSON` call. – T.J. Crowder Jul 26 '11 at 10:52
  • 1
    possible duplicate of [jQuery Call to WebService returns "No Transport" error](http://stackoverflow.com/questions/5241088/jquery-call-to-webservice-returns-no-transport-error) – T.J. Crowder Jul 26 '11 at 10:58

1 Answers1

7

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 :

Community
  • 1
  • 1
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Hmmm... So does that mean that 1.6.2 has security updates now? Btw, the proj of the webservice is on the same vs2010 project, but I think your comment still applies. XD ~ update ~ You're right, I enabled `support.cors` and it works now. I think I should redesign my proof of concept to not use a separate webservice that's on the same machine. – Erick Garcia Jul 26 '11 at 10:51
  • @Erick I've edited my answer with a bit more informations, and a few links that point to something interesting ;-) – Pascal MARTIN Jul 26 '11 at 10:54