Form data will not post - AngularJS

时间:2021-12-22 15:59:35

The code I am using:

我正在使用的代码:

Angular side

角侧

vm.onSubmit =function(){
            var person = vm.formData.slice(0, 1)[0]; //This takes the required fields from the model object nested in an array.
                $http({
            method: 'POST',
            url: 'api/register',
            data: person,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }});

Server side

服务器端

        module.exports.registerPost = function (req,res){
       if(!req.body.email || !req.body.password){
           res.json({success: false, message: 'Please enter an email and password to register.'});
       } else {
           var newUser = new User({
               firstName: req.body.firstName,
               lastName: req.body.lastName,
               email: req.body.email,
               password: req.body.password
       });

The post sends, but I get the error that I haven't enter username or password. Everything else checks out. Doing the call with postman shows the server side is not the issue.

邮件发送,但我收到错误,我没有输入用户名或密码。其他一切都会结束。使用邮递员进行呼叫显示服务器端不是问题。

1 个解决方案

#1


0  

I answered my own question.

我回答了自己的问题。

It seems "data" in the $http object references the request. I wrote it that way because I thought I had to modify the content-type on the headers, but apparently I didn't.

似乎$ http对象中的“data”引用了请求。我这样写是因为我认为我必须修改标题上的内容类型,但显然我没有。

I changed my code to:

我将代码更改为:

vm.onSubmit =function(){
        var person = vm.formData.slice(0, 1)[0];
            $http.post('api/register', person);
}}

And now it works just fine now.

而现在它现在工作得很好。

#1


0  

I answered my own question.

我回答了自己的问题。

It seems "data" in the $http object references the request. I wrote it that way because I thought I had to modify the content-type on the headers, but apparently I didn't.

似乎$ http对象中的“data”引用了请求。我这样写是因为我认为我必须修改标题上的内容类型,但显然我没有。

I changed my code to:

我将代码更改为:

vm.onSubmit =function(){
        var person = vm.formData.slice(0, 1)[0];
            $http.post('api/register', person);
}}

And now it works just fine now.

而现在它现在工作得很好。