jQuery跨域请求解决方法

来源:互联网 发布:java http 发送文件 编辑:程序博客网 时间:2024/05/12 09:20

var xmlHttp = false;
try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
 try {
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 } catch (e2) {
     xmlHttp = false;
 }
}

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}

function sendRequest(p){

    var url = "aaa.bbb.com";

    xmlHttp.open("GET", url, true);

    //xmlHttp.onreadystatechange = updatePage;

    xmlHttp.send(null);

}

如果当前站点域名和请求的站点不属于同一个域名,会提示"没有权限",即限制跨域访问。

可以通过这种方式解决:

function sendRequest(p){

  var url = "aaa.bbb.com"; 
  $.ajax({
      async:false,
      url:url,
      type:"GET",//POST不支持跨域
      dataType:"jsonp",
      jsonp: 'jsoncallback',
      data: "",
      timeout: 100
 });
}

原创粉丝点击