Skip to content
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

Successful response data getting silently tossed due to a JSON parse error? #1516

Closed
fairps opened this issue May 3, 2018 · 3 comments
Closed

Comments

@fairps
Copy link

fairps commented May 3, 2018

Summary

We ran into an issue I thought I'd let y'all know about in case it turns out to be a bug. I am making a call:

axios.post('/foo/.svc/r/GetHappyStuff', {
  <arguments go here>
},{
  cancelToken: cancelToken.token
}).then((resp) => {
  if (resp.data) {
    const res = resp.data.GetHappyResult
    if (res && res.succeeded) {
      // successful
    } 
  }
}).catch((err) => {
  console.error(err);
});

Looking in the Network tab of Chrome's dev tools I can see that the call completes successfully, and I can see the result in the Preview & Response tabs:
image
image

So the weird thing is that in the .then, I can write resp out the console and see the data. I can write out resp.data and see the same, and the if (resp.data) evaluates to true. As soon as I try and access the .GetHappyResult in any way, such as setting up the res variable, it will suddenly have a value of undefined.

What we have determined through pain / trial / error is that the contents of GetHappyResult are failing to parse through the JSON parser because there are some values which got written as NaN which causes the parser to barf... but it looks like that is being eaten and we happily try and use the value down the road where things fail.

Anyway, we've fixed this but I wanted to share our experience since this has been pretty painful and maybe something could be done to expose this kind of error. :-)

Context

  • axios version: v. 0.15.0
  • Environment: Node v6.11.1, Chrome Version 66.0.3359.139 (Official Build) (64-bit), windows 7
@liveKang
Copy link

liveKang commented May 4, 2018

Yes, i also find this issues.
I find the way to slove the problem that: in axios/defaults.js and then change the code like this:

transformResponse: [function transformResponse(data) {
    /*eslint no-param-reassign:0*/
    if (typeof data === 'string') {
      try {
        if (!String.prototype.trim) {
          String.prototype.trim = function () {
            return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
          };
        }
        data = JSON.parse(data.trim(''));
      } catch (e) { 
        /* Ignore */
        console.log('can not JSON.parse the response', e)
      }
    }
    return data;
  }],

hope this can help you!
ENV:
axios version: ^0.18.0
Node: v8.11.1
MAC

@fairps
Copy link
Author

fairps commented May 4, 2018

Thanks so much for the response. That's a great solution! I'd lean more towards a console.error personally though. Any chance this could get officially included? Have you created a PR?

@chinesedfan
Copy link
Collaborator

Closed in favor of #61.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants