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?
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.
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.
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.
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 totrue
if a browser can create anXMLHttpRequest
object and if thatXMLHttpRequest
object has awithCredentials
property.
To enable cross-domain requests in environments that do not supportcors
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With