Reputation: 2318
I'm making a web service call using jQuery's $.ajax to a server on a different domain which I do not control. Unfortunately, the server responds with JSON, not JSONP. So I get Unexpected Token :
error when my callback tries to execute the JSON without a callback "padding" it( JSON by itself is not valid Javascript).
I want to know if there's a way to use $.ajax to make a cross-domain GET request and ignore the response. I really only need one-way communication here.
Code snippet:
$.ajax(
{
url: 'http://service.somedomainidontcontrol.com',
method: 'GET',
dataType: 'jsonp',
success: function(){
e_log("Success");
},
data: my_data
}
);
Server Response(which I would like to ignore):
{"status":"success"}
It occurred to me that I could perhaps append an iFrame to the DOM to accomplish this but I really want a cleaner solution. Also, I'd prefer not to rely on another server to proxy the request.
Upvotes: 1
Views: 1962
Reputation: 61
WARNING. This is old, but I wanted to point out for others finding this that for the img solution - this is no longer "asynchronous". What that means is that if the server is slow in responding, you can delay your page load time.
If you use the img tag solution, be sure to hook it to page complete or take other steps to not impact site performance.
Upvotes: 0
Reputation: 8640
Do I understand you correctly, that you want to send a request to a server you don't control, but don't care if you get a response?
If that is the case, you can create an image in javascript and use the server address as its source:
var img = new Image();
img.src = 'http://service.somedomainidontcontrol.com';
As soon as you add the src
to the image, it will fire of a request to the server. You will get a response that doesn't make any sense as an image, but who cares. :)
Upvotes: 7