$http PUT 中的错误



我正在建立一个简单的拍卖网站。我在页面中展示了我的产品。现在我想提出一个新的报价。在我的控制器中,我使用 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
});
};

在我的模板报价中.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>

在提交新报价中,我通过了拍卖的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 = '';
}
};

就像你看到的我做控制台一样.log看看我传入模板的数据是否正常。他们是! 如果新出价低于我通过的最高出价,我会发送该警报,否则我会提出新出价。

一切正常,所有控制台.log都很好。但是当我提交 newBid(高于旧出价)时,它会向我发送此错误:

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

角度.js:14516 可能未处理的拒绝:{"data":">

意外的令牌 8

400

语法错误:意外的标记 8 at parse (/Users/m.pagano/WebstormProjects/Challenge_2/Project2/sampsite/node_modules/body-parser/lib/types/json.js:83:15) at/Users/m.pagano/WebstormProjects/Challenge_2/Project2/sampsite/node_modules/
body-parser/lib/read.js:116:18 at invokeCallback (/Users/m.pagano/WebstormProjects/Challenge_2/Project2/sampsite/node_modules/raw-body/index.js:262:16) at done (/Users/m.pagano/WebstormProjects/Challenge_2/Project2/sampsite/node_modules/raw-body/index.js:251:7) at IncomingMessage.onEnd (/Users/m.pagano/WebstormProjects/Challenge_2/Project2/sampsite/node_modules/raw-body/index.js:307:7) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9)
","status":400,"config":{"method":"PUT","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/followauctions/newoffer/58dbd190b8042b080d1418bf","data":800,"headers":{"Accept":"application/json, text/plain,/","Content-Type":"application/json;charset=utf-8"}},"statusText":"Bad Request"}

您错误地使用了该方法。

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

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

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

您可以从后端的POST访问newBid

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

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

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

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

最新更新