简体   繁体   English

Javascript:跨域JSON请求问题

[英]Javascript: Cross-domain JSON request issues

I am trying to request JSON from Google Places API, but I am still getting the cross-domain request error after firstly including: 我正在尝试从Google Places API请求JSON,但在首先包括以下内容后,我仍然遇到跨域请求错误:

<?php 
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Allow-Headers: x-requested-with");
?>

The JSON request I am using is standard JQuery: 我使用的JSON请求是标准的JQuery:

function load() {
    var url = 'https://maps.googleapis.com/maps/api/place/details/json?reference=CnRhAAAARMUGgu2CeASdhvnbS40Y5y5wwMIqXKfL-n90TSsPvtkdYinuMQfA2gZTjFGuQ85AMx8HTV7axABS7XQgFKyzudGd7JgAeY0iFAUsG5Up64R5LviFkKMMAc2yhrZ1lTh9GqcYCOhfk2b7k8RPGAaPxBIQDRhqoKjsWjPJhSb_6u2tIxoUsGJsEjYhdRiKIo6eow2CQFw5W58&sensor=true&key=xxxxxxxxxxxxxx';
    $.ajax(url, {
       async:   true,
       success: function(data, textStatus, jqXHR) {
           dump(data);
       }
    });
}

I would use a JSONP query instead, but the Google Places API doesn't support JSONP...how can I solve this? 我会使用JSONP查询,但Google Places API不支持JSONP ...我该如何解决这个问题? With a proxy server? 使用代理服务器? I am not sure how to go about this or what I'm doing wrong. 我不知道如何解决这个问题或者我做错了什么。

The URL you are requesting data from has to grant permission with access control headers. 你是请求数据的URL有权授予访问控制权限头。 It would defeat the object of the same origin policy is a remote origin could grant permission to itself! 它会击败同一起源政策的对象是远程起源可以授予自己的权限!

If the API you are using doesn't provide a JSON-P API, and doesn't set access control headers itself, then you need to use a proxy. 如果您使用的API未提供JSON-P API,并且本身未设置访问控制标头,那么您需要使用代理。 Either one you run yourself, or a third party one that will convert to JSON-P (such as YQL ). 您自己运行的,或者将转换为JSON-P的第三方(例如YQL )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM