【问题标题】:Send json data to nodejs server将json数据发送到nodejs服务器
【发布时间】:2015-03-07 17:55:20
【问题描述】:

我在向我的节点服务器发送 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 有

{ '{"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 }));

感谢您的帮助

【问题讨论】:

    标签: json node.js


    【解决方案1】:

    如果你想以 JSON 格式发送,那么你可以考虑这样做:

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

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 2021-10-15
      • 2011-07-28
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多