2

Ajax call

 $( "#day").datepicker({
    onSelect: function(request) {
        $.ajax({
            type: "POST",
            url: '${pageContext. request. contextPath}/url.htm',
            data: JSON.stringify({
                id: '${someId}'
            }),
           dataType: 'json',
            contentType: "application/json;charset=utf-8",
            success: function (response) {
                if(response.b === true) {
                    $("#fruit").val(response.a);
                }
            }
        }).fail(function(xhr, status, error){
            console.log('error:' + status + ':' + error + ':' + xhr.responseText);
        });
    }
    });

String response from ajax call is as below

{
  "a": "apple",
  "b": true
}

I have tried reading it using var json = $.parseJSON(response); and I get exception Uncaught SyntaxError: Unexpected token o

console.log(response); shows data on console as

Object {
  "a": "apple",
  "b": true
}

I want to fetch value of "a" and "b". How can this be achieved?

8
  • 4
    there is no need to parse it again, use response.a and response.b to access value of a and b Commented Jan 19, 2014 at 10:53
  • response is not a string it seems. Do a typeof response and you will see that it is already an object created automatically from the json response. Commented Jan 19, 2014 at 10:58
  • Than it is not a string. You can access it's properties directly just like @PranavRam said. Or alternatively you can use the indexer operator like this: response["a"] and response["b"]. Commented Jan 19, 2014 at 11:00
  • share your code of ajax request .......... Commented Jan 19, 2014 at 11:02
  • set dataType:'json' Commented Jan 19, 2014 at 11:12

3 Answers 3

2

It's already on JSON format. you don't need to parse it anymore.

use it like this.

  response.a;
  response.b;
Sign up to request clarification or add additional context in comments.

Comments

2

Please review it I have done some changes :) if any query please ask me

$( "#day").datepicker({
        onSelect: function(request) {
            $.ajax({
                type: "POST",
                url: '${pageContext. request. contextPath}/url.htm',
                data: JSON.stringify({
                    id: '${someId}'
                }),
                dataType: 'json',
                success: function (response) {
                    if(response['b'] === true) {
                        $("#fruit").val(response['a']);
                    }
                }
            }).fail(function(xhr, status, error){
                console.log('error:' + status + ':' + error + ':' + xhr.responseText);
            });
        }
        });

1 Comment

With JSON datatType, tried both forms: console.log(response['d']); and console.log(response.d);; Still gets undefined
1

check this:

var ajaxResult = '{"a":"apple","b": true}';

var json= $.parseJSON(ajaxResult );

console.log(json.a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.