Как мне реализовать многострочный flexbox?

У меня такая же проблема в Node.js с JSON.parse().

var https = require('https');
var requestData = {
  "getCustomerProfileIdsRequest": {
    "merchantAuthentication": {
      "name": "your-auth-name-here",
      "transactionKey": "your-trans-key-name-here"
    }
  }
};
var requestString = JSON.stringify(requestData);
var req = https.request({
  host: "apitest.authorize.net",
  port: "443",
  path: "/xml/v1/request.api",
  method: "POST",
  headers: {
    "Content-Length": requestString.length,
    "Content-Type": "application/json"
  }
});

req.on('response', function (resp) {
  var response = '';

  resp.setEncoding('utf8');
  resp.on('data', function(chunk) {
    response += chunk;
  });
  resp.on('end', function() {
    var buf = new Buffer(response);
    console.log('buf[0]:', buf[0]); // 239 Binary 11101111
    console.log('buf[0] char:', String.fromCharCode(buf[0])); // "ï"
    console.log('buf[1]:', buf[1]); // 187 Binary 10111011
    console.log('buf[1] char:', String.fromCharCode(buf[1])); // "»"
    console.log('buf[2]:', buf[2]); // 191 Binary 10111111
    console.log('buf[2] char:', String.fromCharCode(buf[2])); // "¿"
    console.log('buf[3]:', buf[3]); // 123
    console.log('buf[3] char:', String.fromCharCode(buf[3])); // "{"

    // Note: The first three chars are a "Byte Order Marker" i.e. `BOM`, `ZERO WIDTH NO-BREAK SPACE`, `11101111 10111011 10111111`

    response = JSON.parse(response); // Throws error: 'Unrecoverable exception. Unexpected token SyntaxError: Unexpected token'
    console.log(response);
  });
});
req.on('error', function (error) {
  console.log(JSON.stringify(error));
});

req.on('socket', function(socket) {
  socket.on('secureConnect', function() {
    req.write(requestString);
    req.end();
  });
});

Если я назову trim() в ответе, он работает:

response = JSON.parse(response.trim());

Или замените BOM:

response = response.replace(/^\uFEFF/, '');
response = JSON.parse(response);
30
задан Gael 3 April 2016 в 03:16
поделиться