角美元http。不发送任何数据

时间:2021-12-03 11:27:15

I'm super stuck with trying to simply post JSON data but for some reason it won't work.

我非常执着于仅仅发布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);
      });
    }

  })

When I inspect the request in the Chrome inspector it doesn't seem like any data is actually being posted:

当我查看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

As you can see, the X-Error is "Missing consumer key" which implies the data is not being posted correctly.

正如您所看到的,X-Error是“丢失的消费者密钥”,这意味着数据没有被正确地发布。

1 个解决方案

#1


1  

Add this line to your code;

将这一行添加到代码中;

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

美元http.defaults.headers.post(“内容类型”)=“应用程序/ x-www-form-urlencoded”;

Modified code will be like this;

修改后的代码是这样的;

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

angular.module(“口袋里。控制器,[].控制器。控制器('ArticleList',函数($scope, $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);
  });
}

})

})

#1


1  

Add this line to your code;

将这一行添加到代码中;

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

美元http.defaults.headers.post(“内容类型”)=“应用程序/ x-www-form-urlencoded”;

Modified code will be like this;

修改后的代码是这样的;

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

angular.module(“口袋里。控制器,[].控制器。控制器('ArticleList',函数($scope, $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);
  });
}

})

})