簡體   English   中英

表單數據將不會發布-AngularJS

[英]Form data will not post - AngularJS

我正在使用的代碼:

角邊

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

服務器端

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

帖子發送了,但出現錯誤,我沒有輸入用戶名或密碼。 其他所有檢查。 用郵遞員進行呼叫表明服務器端不是問題。

我回答了我自己的問題。

$ http對象中的“數據”似乎引用了該請求。 我這樣寫是因為我認為我必須修改標題上的內容類型,但是顯然我沒有。

我將代碼更改為:

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

現在它可以正常工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM