将json数据发送到nodejs服务器

时间:2015-03-07 17:55:21

标签: json node.js

将json数据发送到节点服务器时遇到问题

我试过

var req = {
        method: 'POST',
        url: 'http://33.33.33.15/user/signin',
        headers : {
      'Content-Type' : 'application/x-www-form-urlencoded'
    },
        data: {test:"test"}
 };

当我console.log() req.body y

{ '{"test":"test"}': '' }

当我尝试

var req = {
        method: 'POST',
        url: 'http://33.33.33.15/user/signin',
        headers : {
      'Content-Type' : 'application/x-www-form-urlencoded'
    },
        data: 'test=test'
 };

我在服务器上有一个好结果

我将内容类型设置为application / x-www-form-urlencoded以允许跨域

我在服务器上

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

如果您想将其作为JSON发送,那么您可以考虑这样做:

var req = {
    method: 'POST',
    url: 'http://33.33.33.15/user/signin',
    headers : {
        'Content-Type' : 'application/json'
    },
    data: JSON.stringify({ test: 'test' })
};
相关问题