为什么我会看到一个"产地不受访问控制 - 允许 - 产地和QUOT允许的;错误在这里? [英] Why am I seeing an "origin is not allowed by Access-Control-Allow-Origin" error here?

查看:169
本文介绍了为什么我会看到一个"产地不受访问控制 - 允许 - 产地和QUOT允许的;错误在这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到以下错误:

Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin

本code:

with this code:

var http = new getXMLHttpRequestObject();
var url = "http://gdata.youtube.com/action/GetUploadToken";
var sendXML = '<?xml version="1.0"?><entry xmlns="http://www.w3.org/2005/Atom"'+
    'xmlns:media="http://search.yahoo.com/mrss/'+
    'xmlns:yt="http://gdata.youtube.com/schemas/2007">'+
    '<media:group><media:title type="plain">My First API</media:title>'+
    '<media:description type="plain">First API</media:description>'+
    '<media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>'+
    '<media:keywords>first, api</media:keywords></media:group></entry>';
http.open("POST", url, true);
http.setRequestHeader("Authorization", "AuthSub token=" + AccessToken);
http.setRequestHeader("X-GData-Key", "key="+ dev_key);
http.setRequestHeader("Content-Type", "application/atom+xml; charset=UTF-8");

http.onreadystatechange = function() {
    if(http.readyState == 4) {
        alert(http.responseXML);
    }
}
http.send(sendXML);

是什么原因,如何解决这个问题呢?

What can cause this, and how do I solve it?

推荐答案

使得当前域之外的Ajax请求时,JavaScript是有限的。

Javascript is limited when making ajax requests outside of the current domain.

  • 例1:你的域名是example.com,你想请求test.com =>你不能。
  • 例2:你的域名是example.com,你想请求inner.example.com =>你不能
  • 出3:你的域名是example.com:80和你想请求example.com:81 =>你不能
  • EX 4:你的域名是example.com,你想请求example.com =>您可以

JavaScript是由同源策略出于安全原因限制,使得恶意脚本无法连接远程服务器并发送敏感数据。

Javascript is limited by the "same origin policy" for security reasons so that a malicious script cannot contact a remote server and send sensitive data.

<一个href="http://www.google.it/url?sa=t&rct=j&q=jsonp&source=web&cd=1&ved=0CDgQFjAA&url=http://en.wikipedia.org/wiki/JSONP&ei=N_g8T-CKLsKhOrW_xcEK&usg=AFQjCNF9iru2-pOWuj8FjOdl2X4WcsD3ZQ&sig2=KlUBhMqKwmdiCjIkxH4nxA">jsonp是一种不同的方式来使用JavaScript。你做出请求和结果被封装入其中运行在客户端的回调函数。这是相同的一个新的脚本链接到你的HTML的头部(你知道,你可以从不同的域比你在这里加载脚本)。
但是,要使用JSONP服务器必须正确配置。如果不是这种情况下,你不能使用JSONP,你必须依靠服务器端代理(PHP,ASP,等)。有很多与此主题有关的指南,它只是谷歌!

jsonp is a different way to use javascript. You make a request and results are encapsulated into a callback function which is run in the client. It's the same as linking a new script tag into the head part of your html (you know that you can load scripts from different domains than yours here).
However, to use jsonp the server must be configured properly. If this is not the case you cannot use jsonp and you MUST rely on a server side proxy (PHP, ASP, etc.). There are plenty of guides related to this topic, just google it!

这篇关于为什么我会看到一个&QUOT;产地不受访问控制 - 允许 - 产地和QUOT允许的;错误在这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆