Ajax - cross domain with jsonp and jquery

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am doing a cross-domain request from site1.com to site2.com (both maintained by me)

This is the jquery code on site1.com :

$.ajax({
        type:  POST ,
        url:  http://site2.com/test/carrousel.cfm ,
        cache: false,
        async: true,
        crossDomain: true, 
        dataType: "jsonp",
        success: function (data, status) {
           alert(data);}
        },
        error: function (xhr, textStatus, errorThrown) {
           alert( error );
        }
    });

I can see the request coming in with status 200 in the debugger. The response body also contains the string that I m sending from the server. That string is: "okay"

Strange enough, the error handler is always fired and I can t access the data. I m sending the Access-Control-Allow-Headers and Access-Control-Allow-Origin headers via the server too (I came across some posts asking to do this)

I also get a script error saying OKAY is undefined. OKAY is the string I get as a reply from the server. How come this is happening? And How can I get this cross domain request to succeed?

I m using JQUERY 1.10.2 & IE 10

As you can see I m also using jsonp & the right parameters as defined in the jquery documentation to perform cross domain requests

Answers

You should try Ajax-cross-origin a jQuery plugin.

http://www.ajax-cross-origin.com/ http://www.ajax-cross-origin.com/

$.ajax({
    crossOrigin: true,
    url: url,
    success: function(data) {
        console.log(data);
    }
});

You will be able to use ajax cross domain with no limitation!

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/19384611/cross-domain-with-jsonp-and-jquery

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils