尝试从MEAN应用程序中的注册表单发布,我可以使用Postman成功发布使用x-www-form-urlencoded,但是当我在console.log中时,应用程序中的请求主体是空的。 登记表(部分):
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// make a list to hold state of every color
for (int i = 0; i < colors.length; i++) {
ColorVO colorVO = new ColorVO();
colorVO.setName(colors[i]);
colorVO.setSelected(checkedColors[i]);
colorList.add(colorVO);
}
// Do something here to pass only arraylist on this both arrays ('colors' & 'checkedColors')
builder.setMultiChoiceItems(colors, checkedColors, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// set state to vo in list
colorList.get(which).setSelected(isChecked);
Toast.makeText(getApplicationContext(),
colorList.get(which).getName() + " " + isChecked, Toast.LENGTH_SHORT).show();
}
});
builder.setCancelable(false);
builder.setTitle("Your preferred colors?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
txtSelected.setText("Your preferred colors..... \n");
// save state of selected vos
ArrayList<ColorVO> selectedList = new ArrayList<>();
for (int i = 0; i < colorList.size(); i++) {
ColorVO colorVO = colorList.get(i);
colors[i] = colorVO.getName();
checkedColors[i] = colorVO.isSelected();
if (colorVO.isSelected()) {
selectedList.add(colorVO);
}
}
for (int i = 0; i < selectedList.size(); i++) {
// if element is last then not attach comma or attach it
if (i != selectedList.size() - 1)
txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName() + " ,");
else
txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName());
}
colorList.clear();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// make sure to clear list that duplication dont formed here
colorList.clear();
}
});
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// make sure to clear list that duplication dont formed here
colorList.clear();
}
});
AlertDialog dialog = builder.create();
dialog.show();
api.js(部分)
<form class="form-horizontal" ng-submit="register.saveUser()">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" ng-model="user.userData.name">
在调用api.js之前,我在server.js中有app.use bodyParser
server.js(part)
var bodyParser = require('body-parser'); // get body-parser
var User = require('../models/user');
var jwt = require('jsonwebtoken');
var config = require('../../config');
var superSecret = config.secret;
module.exports = function(app, express) {
var apiRouter = express.Router();
apiRouter.post('/register', function(req, res) {
var user = new User();
user.name = req.body.name;
user.username = req.body.username;
user.password = req.body.password;
console.log(req.body); //EMPTY
我做错了什么但却看不清楚。
为@mscdex添加: register.saveUser()引用userRegisterController saveUser()函数:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var apiRoutes = require('./app/routes/api')(app, express);
app.use('/api', apiRoutes);
这使用userService注册函数:
.controller('userRegisterController', function(User) {
var vm = this;
// function to create a user
vm.saveUser = function() {
vm.processing = true;
vm.message = '';
// use the register function in the userService
User.register(vm.userData)
.success(function(data) {
vm.processing = false;
vm.userData = {};
vm.message = data.message;
});
};
这会调用我上面发布的api文件。
当我使用Postman时,console.dir(req.headers ['content-type'])显示'application / x-www-form-urlencoded'并且成功。答案 0 :(得分:0)
我今天花了几个小时来完成这个问题,并最终解决了未定义的问题。被apiRouter功能接收是由于我使用了
<input type="text" class="form-control" ng-model="user.userData.name">
而我应该使用
<input type="text" class="form-control" ng-model="register.userData.name">
因为我用过
.when('/register', {
templateUrl: 'app/views/pages/register.html',
controller: 'userCreateController',
controllerAs: 'register'
});
apiRoutes文件中的。 在这种情况下,我仍然在学习ControllerAs的使用。