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?
response.aandresponse.bto access value of a and bresponseis not a string it seems. Do atypeof responseand you will see that it is already an object created automatically from the json response.response["a"]andresponse["b"].dataType:'json'