访问控制 - 允许 - 产地 - 本地主机 [英] Access-Control-Allow-Origin - localhost

查看:112
本文介绍了访问控制 - 允许 - 产地 - 本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,通过AJAX接收JSON,误差小于。据有关资料,我发现迄今就这似乎是某种形式的跨域问题上的错误,但我不知道这意味着什么,以及如何解决它。

I have problems with receiving json through ajax, the error is below. According to the information I've found so far regarding the error this seems to be some kind of cross domain issue, but I have no idea of what that means and how to solve it.

有可能是与响应头的问题(我已经创建了自己的API,并从此之前没有经验),但一个200 OK的,如果直接在浏览器访问URL好评。

There may be an issue with the response header (I have created the API myself and have no experiences since before), however a 200 OK is received if accessing the url directly in the browser.

如果直接在浏览器有效的JSON访问URL示出,以便不应该是问题。

If accessing the url directly in the browser valid json is shown, so that shouldn't be the problem.

这又如何解决呢?

注:URL转到Apache服务器,而不是一直为95%的在这里,我读过有关问题的问题上堆栈的情况下的文件

XMLHttpRequest cannot load http://localhost/api/v1/products?_=1355583847077.
Origin null is not allowed by Access-Control-Allow-Origin.
Error: error 

的code:

    $.ajaxSetup ({
      url: "http://localhost/api/v1/products", // <--- returns valid json if accessed in the browser
      type: "GET",
      dataType: "json",
      cache: false,
      contentType: "application/json"
    })
    $.ajax({
        success: function(data){

            console.log("You made it!");
        },
        error: function(xhr) {
           console.log("Error: " + xhr.statusText);
       }
    }).done(function(data){
        console.log(data);
    })

PARAMS

_ 1355583610778

Params

_ 1355583610778

响应头:

Connection  Keep-Alive
Content-Length  3887
Content-Type    application/json
Date    Sat, 15 Dec 2012 14:50:53 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.1

请求报头:

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3
Connection  keep-alive
Host    localhost
Origin  null
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/17.0 Firefox/17.0

响应

这里没有什么...

Response

Nothing here...

推荐答案

尝试并实现某种形式的 JSONP 机制。如果你使用的是PHP中有可能是一些简单,因为这...

Try and implement some form of JSONP mechanism. If you're using PHP it could be something as simple as this...

/* If a callback has been supplied then prepare to parse the callback
 ** function call back to browser along with JSON. */
$jsonp = false;
if ( isset( $_GET[ 'callback' ] ) ) {
    $_GET[ 'callback' ] = strip_tags( $_GET[ 'callback' ] );
    $jsonp              = true;

    $pre  = $_GET[ 'callback' ] . '(';
    $post = ');';
} //isset( $_GET[ 'callback' ] )

/* Encode JSON, and if jsonp is true, then ouput with the callback
 ** function; if not - just output JSON. */
$json = json_encode( /* data here */ );
print( ( $jsonp ) ? $pre . $json . $post : $json );

这一切会做的是检查一个 $ _ GET 变种名为回调的,然后包装在一个函数调用的输出 - 以< $ C C> $ $ _ GET ['回调'] 名称作为函数名。

All this would do is check for a $_GET var called callback, and then wrap the output in a function call - taking the $_GET['callback'] name as a function name.

那么你的AJAX调用变得像这样......

Then your AJAX call becomes something like this...

$.ajax({
  type: 'GET',
  url: '/* script here */ ', 
  data: /* data here - if any */,
  contentType: "jsonp", // Pay attention to the dataType/contentType
  dataType: 'jsonp', // Pay attention to the dataType/contentType
  success: function (json) {
    /* call back */
  }
});

在jQuery是给定的JSONP作为一个数据类型/的contentType它会为你提供一个回调函数名护理 - 并设置回调函数上升等;这意味着你不必做任何事情!

When jQuery is given 'jsonp' as a dataType/contentType it will take care of providing a callback function name for you - and setting the callback function up etc; meaning you don't have to do anything really!

从jQuery的文档:

JSONP:使用JSONP的JSON块负荷。增加一个额外的?回调=?你的网址的结尾指定回调。禁用缓存通过追加查询字符串参数,_ = [TIMESTAMP],到URL,除非缓存选项设置为true。

"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

来源

在结束; JSONP将是你最好的选择 - 我已经包含PHP code在你的服务器端脚本是用PHP关的机会;如果没有,则原则是相同的。 jQuery的/客户端的东西保持不变,不管服务器端的技术,虽然。 (一般的)

In closing; JSONP is going to be your best bet - I've included PHP code in the off chance that your server side script is using PHP; if not then the principles are the same. The jQuery/client side stuff stays the same regardless of server side technologies though. (in general)

祝你好运:)

这篇关于访问控制 - 允许 - 产地 - 本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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