3

I am trying to access cross-domain data by using jsonp or XMLHttpRequest with GET method. My Code:

XMLHttpRequest

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/ajax.php?code=BSE", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        alert(xhr.responseText);
    }
xhr.send();

JsonP

$.ajax({
     type: 'GET',
     url: "http://example.com/ajax.php?code=BSE",
     dataType: "jsonp",
     jsonpCallback: "jsonp_callback",
     crossDomain: true,
     success: function(res){
         console.log(res);
     }
});

Both methods having same behavior. Whenever i am sending request its just keep loading (even i am not sure its sending request or not) and do nothing.

And my php code:

PHP Code:

header('content-type: application/json; charset=utf-8');
$dts=array('value'=>'123');
echo $_GET['jsonp_callback'] . '('.json_encode($dts).')';
18
  • yes.. all another jquery functions are working fine.. Commented Mar 14, 2017 at 4:50
  • the same XMLHttpRequest working perfect in chrome extension.. Commented Mar 14, 2017 at 4:51
  • jsonpCallback: "jsonp_callback", is this callback function actually exist Commented Mar 14, 2017 at 4:51
  • I hope the edit that was made on your behalf fixed a typo you made, because now, the XHR code is valid Commented Mar 14, 2017 at 4:53
  • @Alive to Die: i do not know much more about jsonp really Commented Mar 14, 2017 at 4:53

2 Answers 2

5

The XMLHttpRequest will work just fine, no need for jsonp. In your manifest.json, make sure you request permissions for the domain you are posting to -- Chrome doesn't require permissions for XHR, but Firefox does. This error manifests in Firefox as a http code 404 in the XHR, but no activity in the network panel. If you get a http code 0, you have CORS or mixed content security issues as well.

{
  "manifest_version": 2,
  "name": "web extension",
  "version": "1",
  "permissions": [
    "http://example.com/"
  ],
  "content_scripts": [
    {
      // ...
    }
  ]
}
2
  • oh god, you saved my life, it is so stealth option... topic caster can mark this answer as needed!
    – Roma Rush
    Commented May 11, 2017 at 21:32
  • Thank you, it is not needed in chrome but it is in Firefox
    – brouille
    Commented Jan 14, 2018 at 18:43
-1

Try using new XDomainRequest() in your xhr request. XDomainRequest is an implementation of HTTP access control (CORS).

var createCORSRequest = function(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Most browsers.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // IE8 & IE9
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
};

var url = 'http://example.com/ajax.php?code=BSE';
var method = 'GET';
var xhr = createCORSRequest(method, url);

xhr.onload = function() {
  // Success code goes here.
};

xhr.onerror = function() {
  // Error code goes here.
};


xhr.setRequestHeader('content-type', 'application/json; charset=utf-8');
xhr.send();

Not the answer you're looking for? Browse other questions tagged or ask your own question.