Angular $ http.post 404和400,但为什么呢?

时间:2015-03-03 07:43:32

标签: javascript angularjs http stripe-payments http-status-code-400

当$ scope.result = result.id发送$ scope.result时,我一直收到400个错误的JSON响应。当$ scope.result = result时,当我传递result.id或$ scope.result时,我得到404,找不到404。我正尝试使用以下代码从Angular发布到我的Express服务器:

angular.module('startupApp')
    .controller('stripeCtrl', function ($scope, $http) {

        $scope.stripeCallback = function (code, result) {
            if (result.error) {
                window.alert('it failed! error: ' + result.error.message);
            } else {
                window.alert('success! token: ' + result.id);
                $scope.result = result.id;
                $http.post('api/stripe', $scope.result)
                    .success(function (response) {
                    // not sure if anything goes here; not looking for a response.
                })
            }
        };
    });

我要传入的对象在JS控制台中看起来像这样:

Object {id: "tok_15cAbpL4TQAw2alUSkdXzIN5", livemode: false, created: 1425368309, used: false, object: "token"…}

卡:对象 client_ip:“76.126.66.106” 创建:1425368309 id:“tok_15cAbpL4TQAw2alUSkdXzIN5” livemode:false 对象:“令牌” 类型:“卡” 用过:假 proto :对象

我正在使用带条纹的Angular Payments。

??????????????????????????????????????????? 更新: 这就是我在前面的代码中完全在JS控制台中获得的内容。

 GET http://localhost:9000/bower_components/lodash/lodash.js 404 (Not Found)
stripe.controller.js:15 Object {id: "tok_15cBUlL4TQAw2alUb8yGnalO"}
stripe.controller.js:16 Object {id: "tok_15cBUlL4TQAw2alUb8yGnalO"}
angular.js:9814 POST http://localhost:9000/api/stripe 404 (Not Found)

和标题:

Remote Address:[::1]:9000
Request URL:http://localhost:9000/api/stripe
Request Method:POST
Status Code:404 Not Found
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:37
Content-Type:application/json;charset=UTF-8
Host:localhost:9000
Origin:http://localhost:9000
Referer:http://localhost:9000/stripe
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Request Payloadview source
{id: "tok_15cBUlL4TQAw2alUb8yGnalO"}
id: "tok_15cBUlL4TQAw2alUb8yGnalO"
Response Headersview source
Connection:keep-alive
Content-Type:text/html
Date:Tue, 03 Mar 2015 08:35:16 GMT
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:Express

以下是显式声明响应头的更新:

angular.module('startupApp')
    .controller('stripeCtrl', function ($scope, $http) {

        $scope.stripeCallback = function (code, result) {
            if (result.error) {
                window.alert('it failed! error: ' + result.error.message);
            } else {
                window.alert('success! token: ' + result.id);
                $scope.result = result.id;
                console.log({id: result.id});
                console.log({id: $scope.result});
                //$http.post('api/card', $scope.card)
                var req = {
                    method: 'POST',
                    url: 'api/stripe',
                    headers: {
                        'Content-Type': application/json
                    },
                    data: {id: $scope.result}
                }
                $http.post(req)
                    .success(function (response) {
                    // not sure if anything goes here; not looking for a response.
                })
            }
        };
    });

这是JS控制台响应:

ReferenceError: application is not defined
    at Scope.$scope.stripeCallback (stripe.controller.js:22)
    at angular-payments.js:793
    at Scope.$get.Scope.$eval (angular.js:14384)
    at Scope.$get.Scope.$apply (angular.js:14483)
    at angular-payments.js:792
    at Object.Stripe.token.a.create.Stripe.request.success ((index):2)
    at Object.<anonymous> ((index):3)
    at Object.Stripe.a._receiveChannelRelay [as onMessage] ((index):2)
    at Object.Stripe.isDoubleLoaded.H.Socket.t.concat.incoming ((index):2)
    at f ((index):2)angular.js:11594 (anonymous function)angular.js:8544 $getangular.js:14485 $get.Scope.$applyangular-payments.js:792 (anonymous function)(index):2 Stripe.token.a.create.Stripe.request.success(index):3 (anonymous function)(index):2 Stripe.a._receiveChannelRelay(index):2 Stripe.isDoubleLoaded.H.Socket.t.concat.incoming(index):2 f

0 个答案:

没有答案