SyntaxError:ajax请求中位置1的JSON中的意外标记o

问题描述 投票:0回答:1

我做ajax调用我的代码看起来像这样

        var Data = {
        name              : $('input[name=name]').val(),
        email             : $('input[name=email]').val(),
        phoneno           : $('input[name=phoneno]').val(),
        password          : $('input[name=password]').val(),
    };
    var data = JSON.stringify(Data);
$.ajax({
        url: "/registeruser",   
        type: "POST",      
        data:  data,      
        dataType: 'json',
        contentType: 'application/json',
        success: function(response)   // A function to be called if request succeeds
        {
          console.log('responsee........', response);
        },
        error: function(jqXHR, textStatus, errorMessage) {
            console.log('handle errpe message',errorMessage); // Optional
        },
    });

我的服务器端nodejs出错了 SyntaxError:位置1的JSON中出现意外的标记o

我的快递路线代码

exports.registeruserController = function(req,res,next){
    console.log('sdasdasdasdasd');
 console.log('request of the user to register',req.body);
}
javascript jquery node.js ajax express
1个回答
1
投票

data不是JSON。

它是一个隐式转换为字符串的对象:

var data = { for: "example" };
var what_you_are_sending = "" + data;
console.log(what_you_are_sending);

服务器正在尝试将其解析为JSON。 [是阵列的开始。 o是一个错误。然后停止。


您需要使用JSON.stringify将对象转换为JSON。

var data = JSON.stringify({ for: "example" });
var what_you_should_send = "" + data;
console.log(what_you_should_send);
© www.soinside.com 2019 - 2024. All rights reserved.