$ http PUT中的错误

时间:2017-03-30 16:02:06

标签: angularjs json node.js mongodb

我正在建立一个简单的拍卖网站。我在页面中展示了我的产品。现在我想提出新的报价。在我的控制器中,我使用ng-dialog打开弹出窗体,然后我在需要的拍卖中传递。像这样:

     $scope.clickToOpen4 = function (followingauction) {

    console.log(followingauction.allbids[followingauction.allbids.length -1].bid);
    var newScope = $scope.$new();
    newScope.auction = followingauction;
    ngDialog.open({template: '../views/partials/offer.html',
        className: 'ngdialog-theme-default',
        scope: newScope
    });
};

在我的模板offer.html中,我有一个简单的表单:

      <form name="Offer"  ng-controller="FollowingAuctionsController">
Your Offer:<br>
<input type="number" placeholder="Your Offer" ng-model="newOffer" name="new offer" required> €<br>

<input class="btn" type="submit" ng-click="submitNewOffer(auction._id, auction.allbids[auction.allbids.length -1].bid)" value="OK"><br>

在submitNewOffer中,我传递了拍卖的ID和最高报价。

这是submitnewOffer():

     $scope.submitNewOffer = function (id, bid) {
    console.log(id);
    console.log(bid);
    var newBid = $scope.newOffer;
    if (newBid>bid) {
        console.log(newBid);
        console.log('/api/followingauctions/newoffer/' + id);
        $http.put('/api/followingauctions/newoffer/' + id, newBid)
            .then(function () {
                alert('Offert Done!');
                getFollowingAuctions();
            });
    }

    else {
        alert('The offer must be higher!');
        $scope.newOffer = '';
    }
};

就像你看到我做console.log一样,看看我传入模板的数据是否正常。他们是! 如果新出价小于我通过的最高出价,我会发送该提醒,否则我会提出新要约。

一切正常,所有console.log都没问题。但是,当我提交newBid(高于旧出价)时,它会向我发送此错误:

angular.js:12578 PUT http://localhost:3001/api/followingauctions/newoffer/58dbd190b8042b080d1418bf 400(错误请求)

angular.js:14516可能未处理的拒绝:{&#34;数据&#34;:&#34;

意外的令牌8

400

{{ 2}}&#34;&#34;状态&#34; 400&#34;配置&#34; {&#34;方法&#34;:&#34; PUT&#34;&#34 ; transformRequest&#34;:[空],&#34; transformResponse&#34;:[空],&#34; jsonpCallbackParam&#34;:&#34;回调&#34;&#34; URL&#34 ;: &#34; / API / followingauctions / newoffer / 58dbd190b8042b080d1418bf&#34;&#34;数据&#34; 800&#34;头&#34; {&#34;接受&#34;:&#34; application / json,text / plain, / &#34;,&#34; Content-Type&#34;:&#34; application / json; charset = utf-8&#34;}} ,&#34; statusText&#34;:&#34;错误请求&#34;}

2 个答案:

答案 0 :(得分:1)

使用$ http执行PUT请求时,您必须指定params

$http.put('/api/followingauctions/newoffer/' + id, {}, params: {'newBid': newBid})
    .then(function () {
        alert('Offert Done!');
        getFollowingAuctions();
});

答案 1 :(得分:1)

您使用的方法不正确。

$http.put方法的正确签名是put(url, data, [config]);

因此,您的代码可能如下所示:

$http.put('/api/followingauctions/newoffer/' + id, {newBid: newBid})
        .then(function () {
            // success
        });

您可以从后端newBid访问POST

OR

$http.put('/api/followingauctions/newoffer/' + id, {}, {params: {newBid: newBid}})
        .then(function () {
            // success
        });

在这种情况下,可以从后端newBid获取GET