码迷,mamicode.com
首页 > Web开发 > 详细

ajax 跨域解决方案

时间:2016-05-07 17:54:48      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

有的浏览器不支持跨域访问,为ie8。 那么我们可以通过重新构造一个新的可以url来支持跨域获取资源,代码如下。

var url = forexample.test.com.cn;

if (!$.support.cors) {
var start = url.indexOf(‘.test.com.cn‘) + 12; //12 为 ".test.com.cn"的长度。
    var callName = url.substring(start);
proxyUrl = url.substring(0, start);
url =
window.location.protocol + "//" + window.location.host + ‘/proxy/?targetUrl=‘ + callName;
}
$.ajax({
type: ‘GET‘, // or ‘POST‘
url: url,
dataType: "json",
contentType: "application/json",
beforeSend: function(request) {
},
success: function(res, status, xhr) {
},
error: function(res) {
}
});


需要注意的是你的后台也要配置跨域访问方式

 

ajax 跨域解决方案

标签:

原文地址:http://www.cnblogs.com/xiaozhaoaixuexi/p/5468628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!