我正在使用带有节点js的angularjs作为服务器。
我从UI传递数据。
schatApp.controller('registerController', function($scope,$location,$http) {
$scope.RegisterCredentials=function(){
var data={
"firstName":"simhachalamrsdafrds",
"lastName":"ajay",
"username":"asfsd",
"email":"ajayfds14@m.com",
"provider":"local",
"displayName":"amruth"
}
// var json = "{\"firstName\": \"firstName\",\"lastName\": \"lastName\",\"username\": \"username\",\"email\": \"email\"}";
var config = {
headers : {
contentType:'application/json; charset=utf-8',
dataType: 'json'
}
}
$http.post("/register",JSON.stringify(data),config).then(function sucess(result){
// alert(JSON.stringify(result));
},function failure(result){
// alert(JSON.stringify(result));
});
}
});
这是我的后端代码
app.post('/register',function(req,res){
var data={
firstName:'simhachalam',
lastName:'ajay',
username:'asfsdq',
email:'ajayfds14@mq.com',
provider:'local',
displayName:'amruth'
}
console.log("111...............");
//console.log(req.body);
//res.setHeader('Content-Type', 'application/json');
core.account.create('local',data,function(err){
if (err) {
var message = 'Sorry, we could not process your request';
if (err.code === 11000) {
message = 'Email has already been taken';
}
return res.status(400).json({
status: 'error',
message: message
});
}
res.status(201).json({
status: 'success',
message: 'You\'ve been registered, ' +
'please try logging in now!'
});
});
});
我收到400个错误请求。
答案 0 :(得分:0)
很难说出可能是什么问题,你需要做一个console.log(错误)对象。你能在这里为我们提供这些信息吗?
if (err) {
var message = 'Sorry, we could not process your request';
if (err.code === 11000) {
message = 'Email has already been taken';
}
return res.status(400).json({
status: 'error',
message: message
});
}
上面的代码清楚地表明您正在返回res.status(400)
,您可以将$ http.post更改为以下内容并进行检查。
$http({
method: 'POST',
url: "/register",
data: JSON.stringify(data),
headers: {'Content-Type': 'application/json; charset=utf-8'}
});
希望您的/register endpoint
托管在您为其提供服务的同一台服务器上。