【问题标题】:setting headers and sending data with $http POST to pocket api returns CORS设置标头并使用 $http POST 将数据发送到袖珍 api 返回 CORS
【发布时间】:2016-05-13 17:00:36
【问题描述】:

无法向pocket api发送http post请求以获取请求令牌。我已经有了消费者密钥。问题似乎在于设置标头并在请求中发送数据。在浏览器中查看的请求不显示任何标头和数据。

配置请求

var req = {
        method:'POST',
        url:'https://getpocket.com/v3/oauth/request',
        headers: { 'Content-Type': "application/x-www-form-urlencoded; charset=UTF8",
                    'X-Accept':'application/x-www-form-urlencoded'
         },
         transformRequest: function(obj) {
            var str = [];
            for(var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            return str.join("&");
        },
        data: pocket_data
    };

    $http(req).then(function(result){console.log(result); 
    },function(error){
        console.log(error);
    });

来自浏览器的请求。我不明白为什么请求方法是 OPTIONS 而不是 POST。

Request URL:https://getpocket.com/v3/oauth/request
Request Method:OPTIONS
Status Code:400 Bad Request
Remote Address:52.0.87.91:443

已发送响应标头和请求标头。当我将其设置为 application/x-www-form-urlencoded 时,我不明白为什么内容类型是 text/html 。

 **Response Headers**
view source
Cache-Control:private
Connection:keep-alive
Content-Length:15
Content-Type:text/html; charset=UTF-8
Date:Thu, 04 Feb 2016 07:22:35 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
P3P:policyref="/w3c/p3p.xml", CP="ALL CURa ADMa DEVa OUR IND UNI COM NAV INT STA PRE"
Pragma:no-cache
Server:Apache
Set-Cookie:PHPSESSID=ogqn0s0gmjpo24j6oo6rlj6vm6; path=/
Status:400 Bad Request
X-Error:Missing consumer key.
X-Error-Code:138
X-Source:Pocket
**Request Headers**
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type, x-accept
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:getpocket.com
Origin:http://localhost:9000
Pragma:no-cache
Referer:http://localhost:9000/create

我什至尝试将 content-type 设置为 'application/json' ,但没有成功。 The api documentation for pocket.

【问题讨论】:

    标签: http http-headers pocket


    【解决方案1】:

    我也有同样的问题。看起来 Pocket API 没有启用 CORS,因为响应没有返回 CORS 标头(例如 Access-Control-Allow-Origin: *)

    有点令人惊讶 - 我猜这个 API 主要是为应用程序开发人员构建的,而不是 Web 开发人员?这也解释了为什么它可以很好地与 curl 等命令行工具配合使用,因为它们没有浏览器那样的跨域安全限制。

    这对您来说意味着您必须编写一个服务器端组件,该组件与您的应用在同一个域中充当这些请求的代理。

    【讨论】:

    • 我已经直接询问了 Pocket,他们确认他们没有启用 CORS,但考虑在未来添加它。正如 David 建议的那样,您可以使用 CORS 代理,例如 cors-anywhere
    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2014-08-07
    • 2016-05-09
    相关资源
    最近更新 更多