2016-05-31 12 views
6

Ben CRUD yapıyorum ve benim arka uç için bazı veri göndermek istiyorsanız (node.js) o zaman bir hata alırsınız: angular.js:10765 POST http://localhost:1234/shop/removeProduct/574bf938b16158b40f9c87bc 400 (Bad Request)

senaryo:

$scope.removeProduct = function (partnerId, productId) { 
    $http.post("/campaign/removeProduct/" + partnerId, productId); 
} 

çözüm sadece basit paketi olan Bu böyle bir nesne parametresi (productId):

$scope.removeProduct = function (partnerId, productId) { 
    $scope.productData = {productId: productId}; 
    $http.post("/campaign/removeProduct/" + partnerId, $scope.productData); 
} 

Ama neden böyle yapmak zorunda? Bu arada, bu doğru mu yoksa farklı bir şekilde mi yapmalıyım?


@EDIT Bir şey daha, nasıl verilerinin yenilenmesi gerektiğini ekledim sonra/herhangi bir nesne kaldırıldı ? Bu doğru mu?

$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; 
}); 
}; 

arka uç:

router.get('/partner-list', function (req, res) { 
    Partner.find({}, function (err, partnerList) { 
     if (err) throw err; 

     res.json({ partnerList: partnerList }); 
    }); 
}); 
+2

Neden sunucu kodu silmek gerçekleştirmek için yük w/o url ve yük hem arka uç kullanımını 'DELETE' fiil id Can ihtiyacı var –

cevap

1

I varsayarak url /shop/removeProduct/34523543?productData=5325345 gibi bir şey olmak istiyorum. o halde ben $http isteği ilan açısal yolu kullanırsınız varsa: text/plain Content-Type başlığını belirlesin

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

+0

teşekkür ederiz Bir soru daha?. Daha önce de söylediğim gibi CRUD. Bu durumda bazılarını kaldırmak istiyorum. İş Ortağı Belgesindeki Diziden Ürün. Bu yüzden 'partnerId' ve' productId' göndermem gerekiyor. Şu anda '' url/url/removeProduct/'+ partnerId''de 'partnerId' gönderdim, ancak doğru mu? Belki bu '$ http ({method: 'POST', url, params: {'partnerData': partnertId, 'productData': productId}})' 'partnerId' ve' productId' göndermeliyim ' – DiPix

+1

Eh,' Gerçekten arka ucunun nasıl olduğunu söyleyeyim. CRUD ise, $ http ({method: 'DELETE', url, params: {'partnerData': partnertId, 'productData': productId}}) 'veya benzeri olmalıdır. – thepio

+0

Her zaman arka planını değiştirebilirim :) – DiPix

1

Metin gönderirseniz:

$scope.removeProduct = function (partnerId, productId) { 
    var config = { headers: { "Content-Type": "text/plain" }}; 
    $http.post("/campaign/removeProduct/" + partnerId, productId, config); 
} 

Bu shoudl düğüm yol işleyiciniz text/plain içeriğini kabul ederse, çalışma. Eğer vücut ayrıştırıcı kullanıyorsanız (bu özel durumda app.use(bodyParser.text());

eklemek emin olun, böyle kaynak uri için DELETE istek göndermek için daha mantıklı görünmektedir:

$scope.removeProduct = function (partnerId, productId) { 
    $http.delete("/campaign/partners/" + partnerId + '/products/' + productId); 
} 

Ve arka uç halledeyim böyle:

app.get('/campaign/partners/:partner/products/:product', function(req, res){ 
    myAwesomeDB.remove(req.params.product).then(/*...*/); 
}); 
İlgili konular