小编Sha*_*thu的帖子

设置标头并使用 $http POST 将数据发送到口袋 api 返回 CORS

无法向 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);
    });
Run Code Online (Sandbox Code Playgroud)

来自浏览器的请求。我不明白为什么请求方法是 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
Run Code Online (Sandbox Code Playgroud)

发送的响应头和请求头。当我将内容类型设置为 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 …
Run Code Online (Sandbox Code Playgroud)

http http-headers pocket

5
推荐指数
1
解决办法
682
查看次数

标签 统计

http ×1

http-headers ×1

pocket ×1