weixin_33716154 2016-11-26 16:40 采纳率: 0%
浏览 63

意外的令牌u NodeJ

i have a problem with http request to forecast.io, if call a request normally with $.ajax its works well, also if i call the request with ajax-request module gives me that output:

SyntaxError: Unexpected token u in JSON at position 0
    at Object.parse (native)
    at ClientRequest.<anonymous> (/Users/rawnly/Documents/Github/weather/forecast.js:11:21)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at Socket.socketOnEnd (_http_client.js:346:9)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Here the code

const request = require('ajax-request');
const token = "**********";
const chalk = require('chalk');

request({
  url: `https://api.darksky.net/forecast/5884bb7f746da897d43f2189e5f3221a/49,12`,
  method: "GET",
  data: {
     format: "json"
  },
}, function(err, res, body) {
  if ( err ) {
    console.log( chalk.red(err) );
  }
  if ( res ) {
    console.log( chalk.red(res) );
  }

  if ( body !== undefined ) {
    var data = body ? JSON.parse(body) : body;
    console.log(data); // the output is undefined... :(
  }
});

Thanks in advance for the help.

EDIT: error output is => Error: socket hang up

 Before flag as duplicate

I've alreasy seen this question and others but doesn't works...

  • 写回答

1条回答 默认 最新

  • weixin_33695450 2016-11-26 19:33
    关注

    The solution: As suggested by @Aurora0001 I've tried with another library and it works fine.

    评论

报告相同问题?