0

I am trying to parse a response of a string which is a json string. In another page of my web app following code is working fine. but its not working for my current page i am working with. Following is the code:

 $.ajax({
    type: 'POST',
    url: 'http://mywebapp.com/sendnames',
    data: {},
    success: function(result) {
        alert('result: '+result);
        var obj = jQuery.parseJSON(result);
        alert('obj: '+obj);

    // doing rest of stuff  
    }

});

first alert comes and shows right result. result is:

 [
   "Richard",
   "Eric",
   "John"
 ]

but second alert does not come. i checked it, its a valid json. why can not i parse this json with jQuery.parseJSON(). Thanks in advance.

8
  • Does alert('obj: '+obj); show Richard,Eric,John? Commented Aug 31, 2012 at 8:20
  • 2
    It's probably because jQuery has already checked the type and your result is a JSON object already. Try alerting the typeof result and seeing what you get. Or accessing one of the expected properties. Commented Aug 31, 2012 at 8:22
  • @Musa no that alert does not come. thats why i wrote jQuery.parseJSON is not working. Commented Aug 31, 2012 at 8:22
  • i am getting the response i want. i debug jquery using alerts. did not get which console are you talking about? Commented Aug 31, 2012 at 8:27
  • I agree with Alex and do a console.log instead of alert. Console.log(result).Let us know the result Commented Aug 31, 2012 at 8:27

4 Answers 4

2

Try to add return type: dataType : json

$.ajax({
        type: 'POST',
        url: 'http://mywebapp.com/sendnames',
        data: {},
        dataType:'json',
        success: function(result) {
          console.log('result: '+result);        
        // doing rest of stuff  
        }

    });

"json":
Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.) "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

1 Comment

jQuery.parseJSON is still wrong, because dataType:json will cause jQuery do that internally. The result variable will already contain a JS object.
1

Replace $.ajax by $.getJSON. This is guaranteed to trigger $.parseJSON internally, so result will already be the desired JS object.

$.getJSON({
   type: 'POST',
   url: 'http://mywebapp.com/sendnames',
   data: {},
   success: function(obj) {
      alert('obj: '+obj);
      // doing rest of stuff  
   }
});

Comments

0

Try with adding dataType:'text', it will return string as result. and your code will run as you expect.

Comments

0

See the accepted answer here

You're parsing an object. You parse strings, not objects; jQuery.parseJSON only takes strings.

Because $.ajax already parsed the data, result is Javascript object not a string. parseJSON expects a string parameter.

FROM DOCS (more on .ajax() data types here):

The json type parses the fetched data file as a JavaScript object and returns the constructed object as the result data. To do so, it uses jQuery.parseJSON() when the browser supports it; otherwise it uses a Function constructor

.

2 Comments

in your answer stackoverflow.com/questions/8631977/… the json object was declared and intialized client-side here the case is not the same i.e. here the resultant object is from external ajax calls which can be anything !!
I'm guessing http://mywebapp.com/sendnames returns the mime type as JSON, and therefore JQuery.parseJSON() is run on the data automatically by jquery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.