1

I want to send data to an external URL in jQuery. I do not care about the response, nor do I want to read the response. Can someone tell me how I can do this? I was able to successfully send data using this call:

$.get("http://sample-domain.com/testfile.php");

However this does not appear to work in IE. Does anyone have suggestions (or a better way) on how to send data (NO RESPONSE) that will work in every browser?

zach
  • 121
  • 1
  • 5
  • You need to use a JSONP request, however this may cause issues if the returned data is not JavaScript, as it's simply an embedded ` – zzzzBov Aug 24 '12 at 16:03

2 Answers2

0

This is because of security restrictions for ajax requests in browsers

You can create an IFrame with that url in src attribute.

Something like that:

$(document).append("<iframe src='http://sample-domain.com/testfile.php'>")

Hope it helps.

Alexey Ogarkov
  • 2,916
  • 23
  • 28
0

For external sites you need

var jqxhr = $.get('http://sample-domain.com/testfile.php', function() {
    alert("success");
  })
  .success(function() { alert("second success"); })
  .error(function() { alert("error"); })
  .complete(function() { 
    alert ("complete");
  });

This are some recomendations from the jQuery site:

  • Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
  • If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method. Alternatively, as of jQuery 1.5, the .error() method of the jqXHR object returned by jQuery.get() is also available for error handling.
  • Script and JSONP requests are not subject to the same origin policy restrictions.
Jorge Vivas
  • 121
  • 3
  • I tried using this exactly (with a different url), and i get two alerts. error and then complete. – zach Aug 24 '12 at 16:06