移动App API调用在浏览器中工作但在程序中失败(帖子中包含的URL)

时间:2015-07-31 17:59:16

标签: javascript jquery ajax json api

目标:在变量中保存一个JSON参数,以便在JavaScript程序中使用。

问题:URL有效(在浏览器中显示JSON),但下面的AJAX调用失败:成功函数无效。 (从我的测试帐户访问令牌。)

提示将受到高度赞赏。

由于

菲利克斯

$.ajax({
    type: "GET",
    dataType: "jsonp",
    url: "https://api.moves-app.com/api/1.1/user/profile?access_token=7hACUBaguM0UI497MrDKJlvYPHu5813EErwFM6UJ7wURsI2d8iLj1BZ0R7Hru2gH",
    success: function(data) {
        alert("Test"); // Not working
    }
});

3 个答案:

答案 0 :(得分:1)

You need a so called proxy page on the server where you load the page from. The page should be written in serverside language as PHP/Java (or other) where no cross domain restriction. you can call cURL for example. So you can call your proxy page, using ajax but with datatype:'json', not 'jsonp'. In you proxy page you have to do the call to https://api.moves-app.com and return the data as json string to jour script. Looks complex, but actually it isn't.

答案 1 :(得分:1)

我会尽力回答并帮助你。 这不起作用,因为浏览器不允许不同的域异步调用它是一种用于安全原因的机制,它被称为same-domain-policy

您没有看到响应,因为浏览器正在阻止它,如果您使用Firefox插件或像POSTMAN这样的Chrome,您可以看到实际上有响应,我确实从您的链接获得了响应使用邮递员:

{"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如果您愿意,电话会对它起作用,你可以使用它:

<?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

?>

答案 2 :(得分:0)

问题是由于网络安全模型的same-domain policy造成的。您的javascript只能在您的网络应用的同一域中调用资源。因此,除非您的网络应用位于 https://api.moves-app.com ,否则您无法对其进行操作。

您需要了解如何按照Reflective的建议执行跨域调用。看看这些问题是什么以及如何做。

  1. What is JSONP all about?
  2. AJAX cross domain call
  3. How to make cross domain request
  4. 其中一个答案导致创建了这个维基百科条目:JSONP