Issue in JSON.parse() , not working properly

This is how I am getting response from ajax call in json encode format and tried to alert

                  success:function(data){
                       var obj = JSON.parse(data);
                         alert(obj);
                  } 

But its not working properly I am not getting any alert , What should I do to fix it ?

Could you post the entire script please, not just a part of it?

“alert” is not the best way. Use console.log(). Check with it data and obj. It’s could be e.g. invalid JSON in response.

Are you 100% sure your request is returning a status code 200? You’ve put this as the success function, which only fires on a successful request… and it’s (according to you) not firing… so…

Standard Emergency Debugging Step 1: Put console.log’s everywhere (before the parse, after the parse, after the alert)
Standard Emergency Debugging Step 2: Observe the console. The log lines will tell you precisely what’s going on:
No log lines: Your request was not successful.
1 log line: Your parse failed, and you’ve suppressed the error.
1 log line + error: Should be self-explanitory. Your parse failed because of [insert error message here], most likely that your response was not well formatted JSON.
2 log lines: somehow the alert failed.
3 log lines: you’ve suppressed alerts (check your extensions/plugins) and the script ran fine.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.