-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
response.json() gives an error "unexpected end of input" if the data sent back from request is null. #268
Comments
This is a usage question and as such should be asked on sites like SO instead of here. Ideally, the server shouldn't return an invalid (empty) response body to an But if you want to avoid a parse error at all costs, why can't you change your _parseJSON: function(response) {
return response.text().then(function(text) {
return text ? JSON.parse(text) : {}
})
} In any case, the power is in your hands. We can't decide your app's logic for you. |
Hey Mislav, thanks for your response and thanks for the work on the fetch polyfill, it's been a real help. |
This is a super late response, but anyways my 2 cents on this issue is that if you're expecting a null response body, with a status code of > 200, you can always send a 204 (no content) response back from the server and handle it without the use of |
@mislav it's nice , and i think maybe add one step to verify that // https://github.com/douglascrockford/JSON-js/blob/master/json2.js#478
const rxOne = /^[\],:{}\s]*$/;
const rxTwo = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
const rxThree = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
const rxFour = /(?:^|:|,)(?:\s*\[)+/g;
const isJSON = (input) => (
input.length && rxOne.test(
input.replace(rxTwo, '@')
.replace(rxThree, ']')
.replace(rxFour, '')
)
);
_parseJSON: function(response) {
return response.text().then(function(text) {
return isJSON(text) ? JSON.parse(text) : {}
})
} |
@mislav the solution almost works, it is enough to do it like this: _parseJSON(response) { Your version was crashing on my side. |
I also encountered a similar problem, |
@Zoly90 and @AndyZhang7 The solution from @mislav works as expected. It's important to note that if you are using the |
this error maybe server not allowed, if CORS set |
I use this code
|
@antoxi4 sometime is not ur code problem. and whatever use ES5 or ES6. |
@antoxi4 By using catch on resp.json() - you might be silently suppressing genuine parsing errors too. So when there is no data or when data has parsing errors - your code always assumes only no data case |
This is what I have:
What I don't understand and would need some help, it is with the fact that the response has a status 200, then goes to parseJSON but then goes back to checkStatus and throws that error "unexpected end of input" (debugging step by step using the chrome debugger).
My question I guess is how could I check for the body of the request in the
this._parseJSON
function and detect there if it is null and do areturn Promise.resolve(response)
instead ofresponse.json()
I am not sure if that should be posted on some site like SO, if yes I'm sorry for the inconvenience.
Thx for your help
The text was updated successfully, but these errors were encountered: