AJAX 跨域问题 php

原生ajax请求方式:
var xhr = new XMLHttpRequest();  
xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);  
xhr.withCredentials = true; //支持跨域发送cookies
xhr.send();

jquery的ajax的post方法请求:
 $.ajax({
               type: "GET",  //POST方式后台获取数据失败,默认jquery.ajax只支持get,
               url: "http://xxx.com/api/test",
               dataType: 'jsonp',
               xhrFields: {
                      withCredentials: true    
              },   //必须有,详见 http://www.w3.org/TR/XMLHttpRequest/#the-withcredentials-attribute
            crossDomain: true,
           success:function(){
     },
           error:function(){
    }
})
服务器端设置:
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *"); //*代表所有网站,可以指定对应授权访问网址 http://www.xxx.com
posted @ 2015-12-30 18:18  水飝飞天  阅读(215)  评论(0编辑  收藏  举报