我想创建一个混合移动应用程序在后台为节点的JS,它会保存在MongoDB中我的服务器数据工作正常我准备途径处理用户请求如何JSON对象传递给节点JS服务器
我可以使用GET方法从我的服务器获取数据,但是我无法保存使用POST方法从离子用户界面发送的数据。我试图使用POSTMAN发送数据数据已成功保存在MongoDB中,但是当我从移动用户界面发送数据时发生问题
这是一张图片,显示使用POSTMAN发送POST请求的节点js服务器中的结果 和从移动用户发送POST请求interce
并且这数据如何被保存在MongoDB中
这是我的路线在节点服务器上的文件
var Product = require('../models/product');
var express = require('express');
var router = express.Router();
router.route('/products')
.get(function(req, res) {
Product.find(function(err, products) {
if (err) {
return res.send(err);
}
res.json(products);
});
})
.post(function(req, res) {
console.log(req.body);
var product = new Product(req.body);
product.save(function(err) {
if (err) {
return res.send(err);
}
res.send({ message: 'product Added' });
});
});
这是形式
<label class="item item-input">
<input name="1" type="text" ng-model="product.nom" placeholder="nom du produit">
</label>
<label class="item item-input" >
<input name="2" type="text" ng-model="product.unite" placeholder="unité de cette produit">
</label>
...
<div class="item button button-block button-positive" ng-click="createProduct(product)" >
ajouter le produit
</div>
,这是产品的控制器:
app.controller('productController', function($http, $scope) {
$scope.createProduct = function (new_prod){
console.log(new_prod);
var req = {
method: 'POST',
url: "http://localhost:3000/api/products",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: new_prod
};
$http(req)
.then(function(response) {
console.log(response);
});
};
,什么回应说? –
如果它的json,你为什么要发布content-type'application/x-www-form-urlencoded'?尝试'应用程序/ json'而不是 – Robbie
@Gonzalo Pincheira Arancibia说该产品已添加,但保存在没有任何数据的mongodb – Taha