How to make Ajax requests, with Jquery, in different domains?


Is it possible to perform a POST-type request to a url that is not part of the domain of our application?

    type: "POST",
    url: "",
    data: {
        var1: $('#input1').val(),
        var2: $('#input2').val(),
.done(function() {

I'm trying to do something like this, but what I get is a "No Access-Control-Allow-Origin 'header is present on the requested resource."

How to resolve this type of problem?

asked by anonymous 09.04.2014 / 15:09

4 answers


You can enable CORS requests in the browser by using jQuery with the following code: = true;

These requests between different domains have long had some security holes, so browsers have disabled this functionality by default. Currently, modern browsers (Internet Explorer 8+, Firefox 3.5+, Safari 4+, and Chrome) allow this type of request, as long as Cross-Origin Resource Sharing (CORS) is also enabled on the server .

An excellent reference on the subject can be found at the following address:

Another excellent reference on how to enable CORS can be found in this question:

09.04.2014 / 15:20

When it comes to CORS, "incompatibility" and restrictions are something we can not ignore.

An alternative way is to use a php file as an intermediary, playing a proxy role.

You can use CURL:


$curl = curl_init();

$url = '';

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,image/jpeg,image/jpg,image/gif,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: pt-BR,en-US,en,pt;q=0.5";
$header[] = "Pragma: ";

curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('param' => 'value'));

$reponse = curl_exec( $curl );

curl_close( $curl );

echo $reponse;


curl_setopt($curl, CURLOPT_POST, TRUE);
  Here the request was set to POST


curl_setopt($curl, CURLOPT_POSTFIELDS, array('param' => 'value')); .   and here are the parameters param=value

09.04.2014 / 17:08

What content is returned from this request? You could use jsonp, so you can do cross-domain requests;

To see how this works, you can see: link

24.05.2015 / 00:20

Hello, use the following example: ( jquery site )

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  img {
    height: 100px;
    float: left;
  <script src=""></script></head><body><divid="images"></div>

(function() {
  var flickerAPI = "";
  $.getJSON( flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
    .done(function( data ) {
      $.each( data.items, function( i, item ) {
        $( "<img>" ).attr( "src", ).appendTo( "#images" );
        if ( i === 3 ) {
          return false;

22.01.2017 / 23:48