Unexpected Token U Ajax Syntax Error

时光总嘲笑我的痴心妄想 提交于 2020-01-25 21:41:49

问题


I've been having a problem all day sending json data via ajax to Express. My ajax looks like this:

$('#saveClause').click(function () {
    var username = document.getElementById('postUserName').innerHTML;
    var clauseTitle = document.getElementById('modalTitle').innerHTML;
    var clauseDescription = document.getElementById('modalDescription').innerHTML;
    var clauseText = document.getElementById('modalText').innerHTML;

    $.ajax({
        url: "/classes/updateAssignment",
        type: "post",
        dataType: "json",
        data: {
            username: username,
            title: clauseTitle,
            description: clauseDescription,
            text: clauseText
        },
        cache: false,
        contentType: "application/json",
        complete: function () {
          console.log("complete");  
        }, 
        success: function () {
            console.log("success");
        },
        error: function () {
            console.log("Process Error");
        }

    });
});

and my Express Classes routes looks like this:

router.post('/updateAssignment', function (req, res) {
    console.log(req.body.username)
    console.log(req.body.title);
    console.log(req.body.description);
    console.log(req.body.text);
    res.type('json');
    res.send({
        some: JSON.stringify({
            response: 'json'
        })
    });


});

I issued a postman post request to the url with this JSON object:

{
    "username":"testing",
    "title":"123",
    "description":"j456",
    "text":"seven"
}

and Express logged all the details in the console just fine, so it must be a problem with my ajax request as it's giving me an unexpected token u error but I don't know what's causing it. Any ideas?


回答1:


Try removing the contentType: "application/json",

If you used postman with no headers, most likely this is causing the parser to fail.



来源:https://stackoverflow.com/questions/45353686/unexpected-token-u-ajax-syntax-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!