【问题标题】:Send Json to nodejs server将 Json 发送到 nodejs 服务器
【发布时间】:2013-12-27 23:48:22
【问题描述】:

我使用 REST nodejs 服务器和 JavaScript / Zepto 客户端开发了一个应用程序。

我正在尝试将一个 json 字符串从我的客户端发送到我的服务器

这里是客户端代码:

$.ajax({
    type: 'POST',
    url: 'http://localhost:3000/request',
    data: JSON.stringify({test : "test"}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: callback,
    processData : false,
    success : function(){
        console.log("toto");
    },
    error : function(){
        console.log("erreur")
    }
});

还有我的节点代码:

app.post('/request', request.request);

// request.js
exports.request = function(req, res){
    console.log(req.body);
    res.header("Access-Control-Allow-Origin", "*");
    res.send("OK");
}

但是我的节点控制台打印这个:{{test : "test"} : ""}

怎么了?

【问题讨论】:

    标签: ajax json node.js jquery zepto


    【解决方案1】:

    通过阅读zepto documentation,我建议您让zepto 处理数据的编码。

    试试:

    $.ajax({
        type: 'POST',
        url: 'http://localhost:3000/request',
        data: {
            test: "test"
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        ....
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多