开发者

cross-domain $.ajax setRequestHeader [duplicate]

开发者 https://www.devze.com 2023-03-16 21:08 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Overcoming Cross-Domain issues开发者_运维百科
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Overcoming Cross-Domain issues

开发者_运维百科

Is it possible to make a cross-domain $.ajax setRequestHeader ?

I´m trying to do this:

    $.ajax({
    url: 'http://remote_url_here',
    type: 'GET',
    dataType: 'json',
    beforeSend: function(xhr) {
       xhr.setRequestHeader('Authorization', 'TRUEREST username=xxxx&password=yyyy');
    },
    success: function() { alert('Sucess')},
    error: function() { alert('Error'); },
});

And in my "remote_url" im doing a $_SERVER['HTTP_AUTHORIZATION'] but its allways empty. I also tried looking in firebug but no headers are set, same thing if type:POST. Am i doing something wrong?

Thanks in advance!


If you control http://remote_url_here you can via the Access-Control-Allow-Origin header

https://developer.mozilla.org/en/HTTP_access_control

header('Access-Control-Allow-Origin: *');

or

header('Access-Control-Allow-Origin: http://permitted_domain.com');


add crossDomain:true to your $.ajax() params


You can't do a cross-domain ajax request due to javascript's same-origin policy, unless you use JSONP (which isn't useful in your case, I think).

Hope this helps. Cheers

UPDATE Now you can with the crossDomain: true option.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号