JSONP req.body 无效

JSONP req.body is not work

我正在使用 nodejs 和 angular 当我点击 angular 一侧的按钮时它会生成 json post 但由于跨域问题我使用 JSONP .当我尝试获取通过 JSONP 从客户端发送的数据时,我得到了这样的信息;

{ '{"userName": "something", "password" : "123"}': '' }

我在 server.js 上从 req.body 得到它。

这是我的服务器端代码

    app.post('/', function(req,res){
  console.log("consoleeee:" + req.body);
  res.end();
});

这里是我的客户端功能,它在单击按钮时起作用

function loginPageLogin(a, b, c, d, e, f, g, h, i, j) {

    $.ajax
    ({
        type: "POST",
        url: 'http://localhost:777/node',
        dataType: 'jsonp',
        async: false,
        data: '{"userName": "' + a.value + '", "password" : "' + b.value + '"}'
    })

如何才能正确得到 req.body?我想要它作为对象。谢谢...

您需要更改 data 以便它推送对象而不是字符串:

$.ajax
    ({
        type: "POST",
        url: 'http://localhost:777/node',
        dataType: 'json',
        async: false,
        data: {userName: a.value, password: b.value}
    })

无法使用 JSONP 传输 POST json 数据,因为 JSONP 请求是通过将