简体   繁体   中英

Request js library Unexpected end of JSON input

I am running server on node.js and Express v4.0

And i am using request library Uncaught SyntaxError: Unexpected end of JSON input then recving response from server i am getting response but without last symbol ("}") so when i try to parse JSON it throws error. The problem is that if i do same request with xmlhttprequest from Chrome it works with SAME HEADERS AND SAME BODY

request parametrs:

request({
                    method:"POST",
                    jar:true,
                    url:host+"/api/location",
                    form:{
                        longitude:elem.longitude,
                        latitude:elem.latitude
                    },
                    followAllRedirects:true
                },(err,res,body)=>{
                    logger.log(body);
                    body = JSON.parse(body);
                    assert(!body.success);
                    assert(body.longitude != elem.longitude && body.latitude != elem.latitude);
                    done();
                });

response is comming back incorrect code in express:

res.send(JSON.stringify(({
  error:"Unknown",
  errorCode:errorCode,
  success:false,
  "-_-":"_-_"
})));

last fields was a try to send another field and check is other field is malformed or not. and it wasnot malformed but still last symbol not recved.

also i tried to use res.json() with same body but no success. same result.

what i can do? is anyway to find where is problem?

You do not need to stringify the JSON and parse the result. You should be able to just send the JSON as a response and the 'body' in the request callback should contain the JSON from the response.

I fixed it.

There was error on server side that was not visible to some reasons.

res.json().end() was used two times. So body was already sent and i was trying to add something else. This is strange behavoir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM