0
当我尝试发送跨域请求google.docs URL它的工作原理,但是当我尝试把它发送到服务器上的其他领域,它提供了错误:跨域AJAX请求头
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网址:
这里头的谷歌文档的网址:
其次我尝试设置myserve R-本地主机。添加了一些标题来响应,如:
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'} –