ajax请求在Postman中工作,而不在代码中工作

JT·霍克

我有一个Web应用程序向node.js中的服务器发出ajax请求,但是在请求时,我总是在服务器控制台中收到错误消息:

SyntaxError:JSON中位置0处的意外令牌#

但是,在邮递员中,我发送了相同的请求,并且一切顺利。我确定我的ajax请求中有问题,但是我已经为此工作了好几天,但我不知道是什么原因。

这是ajax请求:

$.ajax({
    contentType: 'application/json',
    url: "/insert/image_data",
    method:"post",
    data: {
        tags: tag,
        cats: cat
    },
    dataType: "json",
    success:function(res){
        console.log(res);
    },
    error: function(err){
        console.log(err);
    }
});

这是服务器处理程序:

app.post("/insert/image_data", function(req, res){
    let imageData = req.body;
    console.log("/insert/image_data route reached");
    console.log("--> imageData = ", prettyjson.render(imageData));

    res.status(200).send("Req GOOD");
});
JT·霍克

根据评论的建议,使用JSON.stringify将请求数据转换为有效的JSON格式解决了该问题。这是工作代码:

$.ajax({
    contentType: 'application/json',
    url: "/insert/image_data",
    method:"post",
    data: JSON.stringify({
        tags: tag,
        cats: cat
    }),
    dataType: "json",
    success:function(res){
        console.log(res);
    },
    error: function(err){
        console.log(err);
    }
});

感谢上述解决方案的@gargkshitiz和@DannyDainton。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Http Post在Postman中工作,但不在Flutter中工作

jQuery Ajax请求不在FF和Safari中工作,在Chrome中工作

请求在 curl 中工作但不在 RestTemplate 中工作

重定向到不在ajax请求中工作的非授权用户

在node.js中使用请求模块时,POST请求状态代码(500)。Postman中的POST请求工作正常

卷曲请求在终端中工作,但不在PHP中

POST API如何在Postman中工作而不在改造中工作?

Retrofit 2 在 Postman 和 Localhost 中工作,但不在 Amazon AWS Live Server 上工作

WheelNav 不在 CodePen 上工作,但在代码片段中

单击从控制台工作但不在代码中

GetLogicalDrives - 代码在本地工作但不在虚拟机中

绑定在 XAML 中工作但不在代码后面

为什么是 sep='.' 不在此代码中工作?

我的 SQL 请求在 SQL 中工作,但不在 PHP 中工作

API 在 Postman 中工作但在代码中出错

浏览器问题搜索代码在Firefox中工作,而不在Internet Explorer中工作

代码在jdk7中工作而不在jdk8中工作

Phpmyadmin不在openshift中工作

历史-不在bash中工作

带有Angular和Express的CORS-在Postman中工作,但不在浏览器中工作

.html中的.append不在jquery中工作

ajax请求在laravel中仅工作一次

GWT Regex在DevMode中工作,而不在Production中工作

cd在shell中工作,但不在脚本中工作

CSS在Codepen中工作而不在jsfiddle中工作

Codeigniter CLI在终端中工作,而不在Cpanel cron中工作

Git在bash中工作,但不在Android Studio中工作

在终端中工作,但不在脚本中工作

创建外部表不在 spark 中工作,在 Athena 中工作