【问题标题】:Not well formed Json when sending to CouchDB发送到 CouchDB 时 Json 格式不正确
【发布时间】:2012-12-17 22:21:36
【问题描述】:

在使用 node.js 作为代理以防止 xss 问题时,我在发送 json 时遇到了问题。

在记录收到的数据时,我找不到任何问题。

【问题讨论】:

    标签: javascript ajax node.js couchdb xss


    【解决方案1】:

    当我编写另一个显示接收到的数据的 node.js 服务器并让它模仿 CouchDB 服务器时,我找到了解决方案。

    原来是一个非 ascii 字符(瑞典语-Å)才是罪魁祸首。收到的数据被视为原始计算 Content-Length,或者根据您的心情正确。 ;)

    解决方案是在计算 Content-Length 之前使用 Buffer 将原始数据转换为 utf8。

         :
    if (request.method == 'PUT') {
        var data = '';
        request.on('data', function(dataSnippet) {
            data += dataSnippet;
            if (data.length > 1e6) {request.connection.destroy();}
        });
        request.on('end', function(dataSnippet) {
            data = new Buffer(data, 'UTF8');     //<---  This is the solution
            options.headers = {
                'Content-Encoding': 'UTF8',
                'Content-Type': 'application/json',
                'Content-Length': data.length    //<---  Where it went wrong
            }
            proxy = http.request(options, function(proxy_response) {
                proxy_response.setEncoding('UTF8');
                proxy_response.pipe(response);
            });
            proxy.write(data);
            proxy.end();
        });
    }
        :
    

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多