我会尽力回答并帮助你。
这不起作用,因为浏览器不允许不同的域异步调用它是一种出于安全原因而使用的机制,它被称为same-domain-policy
您看不到响应,因为浏览器阻止了它,如果您使用 REST 插件 for firefox 或 chrome,例如 POSTMAN,您可以看到实际上有响应,我确实使用邮递员从您的链接中获得了响应:
{"userId":1368648652999913,"profile":{"firstDate":"20150202","currentTimeZone":{"id":"Europe/Berlin","offset":7200},"localization":{"language":"en","locale":"en_US","firstWeekDay":1,"metric":true},"caloriesAvailable":false,"platform":"ios"}}
您可以创建一个代理服务器(例如使用 php)为您进行调用,然后使用 jquery 为该代理服务器创建 ajax,我为您制作了一个简单的代理,它可以工作,并且 $.ajax call 将对其起作用,您可以根据需要使用它:
<?php
function executeRequest($url, $parameters = array(), $http_method = 'GET', array $http_headers = null, $form_content_type = 'multipart/form-data', $getResponseHeaders = false)
{
$certificate_file = null;
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_CUSTOMREQUEST => $http_method
);
if ($getResponseHeaders){
$curl_options[CURLOPT_HEADER] = true;
}
switch($http_method) {
case 'POST':
$curl_options[CURLOPT_POST] = true;
/* No break */
case 'PUT':
case 'PATCH':
/**
* Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data,
* while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.
* http://php.net/manual/en/function.curl-setopt.php
*/
if(is_array($parameters) && 'application/x-www-form-urlencoded' === $form_content_type) {
$parameters = http_build_query($parameters, null, '&');
}
$curl_options[CURLOPT_POSTFIELDS] = $parameters;
break;
case 'HEAD':
$curl_options[CURLOPT_NOBODY] = true;
/* No break */
case 'DELETE':
case 'GET':
if (is_array($parameters)) {
$url .= '?' . http_build_query($parameters, null, '&');
} elseif ($parameters) {
$url .= '?' . $parameters;
}
break;
default:
break;
}
$curl_options[CURLOPT_URL] = $url;
if (is_array($http_headers)) {
$header = array();
foreach($http_headers as $key => $parsed_urlvalue) {
$header[] = "$key: $parsed_urlvalue";
}
$curl_options[CURLOPT_HTTPHEADER] = $header;
}
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
// https handling
if (!empty($certificate_file)) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, $certificate_file);
} else {
// bypass ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
if (!empty($curl_options)) {
curl_setopt_array($ch, $curl_options);
}
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if ($curl_error = curl_error($ch)) {
throw new Exception($curl_error, null);
} else {
$json_decode = json_decode($result, true);
}
curl_close($ch);
return array(
'result' => (null === $json_decode) ? $result : $json_decode,
'code' => $http_code,
'content_type' => $content_type
);
}
function getUserProfile($url){
$parameters = array();
$http_headers = array('Accept'=>'application/json',
'Content-Type'=>'application/x-www-form-urlencoded');
$result = executeRequest($url, $parameters, 'GET', $http_headers, 0);
return $result;
}
$url = $_GET['url'];
$result = getUserProfile($url);
echo $result['result'];
//Example usage:
//index.php?url=https://api.moves-app.com/api/1.1/user/profile?access_token=7hACUBaguM0UI497MrDKJlvYPHu5813EErwFM6UJ7wURsI2d8iLj1BZ0R7Hru2gH
?>