I'm making CRUD and if I want to send some data to my backend (node.js) then I receive an error:
angular.js:10765 POST http://localhost:1234/shop/removeProduct/574bf938b16158b40f9c87bc 400 (Bad Request)
script:
$scope.removeProduct = function (partnerId, productId) {
$http.post("/campaign/removeProduct/" + partnerId, productId);
}
The solution is just simply pack this parameter (productId
) in an object like this:
$scope.removeProduct = function (partnerId, productId) {
$scope.productData = {productId: productId};
$http.post("/campaign/removeProduct/" + partnerId, $scope.productData);
}
But why I have to do this like this? By the way, is this correct or should I do it in a different way?
@EDIT One more thing, how should i refresh data after I added/removed any object? Is this correct?
$scope.addPartner = function(data) {
$http({method: 'POST', url: addPartner, data})
.then(function(response) {
console.log(response);
});
$scope.loadPartnersData();
window.alert("Partner added!");
};
$scope.loadPartnersData = function () {
$http.get("/campaign/partner-list").then(function(result) {
$scope.partnerList = result.data.partnerList;
});
};
backend:
router.get('/partner-list', function (req, res) {
Partner.find({}, function (err, partnerList) {
if (err) throw err;
res.json({ partnerList: partnerList });
});
});
queryParams property accepts object as a value. To pass multiple query parameters to router. navigate we can construct an object with list of parametrs and pass it on to queryParams . For example to display books under category fiction and orderby price, use the below code snippet.
Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters. Let's go through an example to understand it further.
To send a path parameter, enter the parameter name into the URL field, after a colon, for example :id . When you enter a path parameter, Postman will populate it in the Params tab, where you can also edit it.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
I'm assuming you want the url to be something like /shop/removeProduct/34523543?productData=5325345
. If so then I would use the angular way of declaring $http
request:
var url = '/shop/removeProduct/' + partnerId; /* How ever you declare this */
$scope.removeProduct = function() {
$http({method: 'POST', url, params:{'productData': productId}})
.then(function(response) {
console.log(response);
});
};
$scope.removeProduct();
Angular then takes care of the decoding of the parameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With