【问题标题】:Cross domain ajax request headers on different servers不同服务器上的跨域ajax请求标头
【发布时间】:2014-03-12 10:36:37
【问题描述】:

当我尝试向 google.docs url 发送跨域请求时,它可以工作,但是当我尝试将其发送到另一个域上的服务器时,它给出了错误:

 XMLHttpRequest cannot load http://katrin.kit.edu/adei/services/getdata.php?db_server=orca&db_name=orca_process&db_group=Data_001_PAC_dat&db_mask=0,1,2,3,4,5,6,7&window=-1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. 

但是当我尝试使用 google.doc 时,它返回正常的解析对象,没有任何错误。

我的要求:

 function ajax(url, callback, filetype, type) {
filetype = filetype ? filetype : 'json';
type = type ? type : 'GET';
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
var success = function(e) {
    var items = '';
    switch(filetype) {
        case 'csv': items = csv(xhr.responseText); break;
        case 'json': items = JSON.parse(xhr.responseText); break;
        default: items = xhr.responseText; break;
    }
    callback(items);
}
var error = function(e) { console.log('Please enabled CORS using  access-control-allow-origin'); }
if (window.XDomainRequest && !sameOrigin(url)) { xhr = new XDomainRequest(); xhr.onload = success; }
if (filetype == 'image' && xhr.overrideMimeType) { xhr.overrideMimeType('text/plain; charset=x-user-defined'); }
xhr.onerror = error;
xhr.onreadystatechange = function(e) { if (xhr.readyState == 4 && xhr.status == 200) { success(e); } }
try {
    if ('withCredentials' in xhr) { xhr.open(type, url, true); }
    else { xhr.open(type, url); }
    xhr.send(null);
}
catch(e) { error(e); }
}

// check if url is same domain

function sameOrigin(url){
   var split = url.split('/');
   if (split[0]+'//' == window.location.protocol+'//') { return split[2] != window.location.host ? false : true; }
   else { return true; }
}

// calculate length of object

function size(obj) {
  var size = 0, key;
  for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
  }
 return size;
}

我尝试更改标题,但仍然存在问题:

这是 myserver url 的标头:

这是 google doc url 的标题:

其次,我尝试设置 myserver-localhost。在响应中添加了一些标头,例如:

def index(request):
data = {
    'title': getattr(settings, 'TITLE'),
    'description': getattr(settings, 'DESCRIPTION')
}
response = render_to_response('dimension/index.html', data, context_instance=RequestContext(request))

response['Access-Control-Allow-Origin'] = '*'  
response['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'  
response['Access-Control-Max-Age'] = '1000'  
response['Access-Control-Allow-Headers'] = '*'  
return response  

但我认为问题与我的本地主机服务器无关。

我尝试了 jsonp 库。它可以工作,但实际上只有 json 文件。但我需要不同的格式,比如 csv。

提前致谢!

【问题讨论】:

  • 我们看不到图片。请张贴文字而不是图片。顺便说一句,可以通过 jsonp 请求获取 csv 文件(“文本”)。事实上,使用 jQuery 非常简单。
  • @OscarPaz,你能不能写在这里,如何使用jsonp解析任何数据类型。因为我用谷歌搜索了很多网站,发现这是不可能的(我的意思是如果 csv 文件放入 1 个 json 对象中是可能的:{ obj1 : 'csv'}

标签: javascript ajax django http-headers xmlhttprequest


【解决方案1】:

要进行跨域请求,您请求的域应该授予您权限,并且该权限在标头中作为对请求的响应发送回浏览器。如果浏览器发现您的名字不在允许的客户端列表中,浏览器会显示错误。因此,您不能在任何域中发出请求。这是为了防止 CSRF-Cross Site Request Forgery。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 2017-02-02
    • 2012-01-30
    • 2012-06-10
    • 2014-11-22
    • 2015-04-28
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多