Angular $ http.post不发送任何数据

问题描述:

我超级坚持试图简单发布JSON数据,但由于某种原因它不会工作。

angular.module('pocket.controllers', []) 
    .controller('ArticleList', function($scope, $http) { 

    $scope.signIn = function() { 

     var postObject = new Object(); 
     postObject.consumer_key = pocketKey; 
     postObject.redirect_uri = "http://www.example.com"; 

     $http.post(apiUrl, postObject).success(function(data){ 
     alert(data); 
     }); 
    } 

    }) 

当我检查在Chrome检查请求它似乎并不像任何数据实际上被张贴:

Request URL:https://getpocket.com/v3/oauth/request 
Request Method:OPTIONS 
Status Code:400 Bad Request 
Request Headersview source 
Accept:*/* 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Access-Control-Request-Headers:accept, origin, x-requested-with, content-type 
Access-Control-Request-Method:POST 
Cache-Control:no-cache 
Connection:keep-alive 
Host:getpocket.com 
Origin:http://pocket.dev:8000 
Pragma:no-cache 
Referer:http://pocket.dev:8000/app/index.html 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36 
Response Headersview source 
Cache-Control:private 
Connection:keep-alive 
Content-Length:15 
Content-Type:text/html; charset=UTF-8 
Date:Wed, 24 Jul 2013 17:18:04 GMT 
P3P:policyref="/w3c/p3p.xml", CP="ALL CURa ADMa DEVa OUR IND UNI COM NAV INT STA PRE" 
Server:Apache/2.2.25 (Amazon) 
Status:400 Bad Request 
X-Error:Missing consumer key. 
X-Error-Code:138 
X-Powered-By:PHP/5.3.27 
X-Source:Pocket 

正如你所看到的,X-错误是“缺少消费者密钥“这意味着数据未被正确发布。

+2

可能的[AngularJS重复执行对跨源资源的OPTIONS HTTP请求](http://*.com/questions/12111936/angularjs-performs-an-options-http-request-for-a-cross-origin-资源) – Stewie

将此行添加到您的代码;

$ http.defaults.headers.post [“Content-Type”] = “application/x-www-form-urlencoded”;

修改后的代码会是这样的;

angular.module( 'pocket.controllers',[]).controller( 'ArticleList', 函数($范围,$ HTTP){

$scope.signIn = function() { 

    var postObject = new Object(); 
    postObject.consumer_key = pocketKey; 
    postObject.redirect_uri = "http://www.example.com"; 
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 
    $http.post(apiUrl, postObject).success(function(data){ 
    alert(data); 
    }); 
} 

})

+0

这也行不通,我认为它有一些事情与他们的服务器不支持移植CORS,因此无法完成跨域POST请求。 – jmohsenin

+0

你是在单个域中执行此操作还是正在进行跨域POST请求? – BKM

+0

不,这是对Pocket API的跨域请求:http://getpocket.com/developer/docs/authentication – jmohsenin