I am sending success response through JSON.parse(), but I am not getting the exact value needed. I want to get values differently from the response. Below is the Jquery code i Laravel View I am using:-
$.ajax({
type: 'GET',
url: '/user/checkout',
cache: false,
data: { pickupName:pickupName,locationId:locationId },
dataType: 'application/json',
success: function (response) {
if (response =='true'){
var obj = JSON.parse(response);
alert(obj.pickup_name);
}
},
error: function (response) {
}
});
Below is the response I am receiving from controller in Laravel:-
{"pickup_name":"abcd","pickup_address":"Chicago, North State Street, Chicago, IL, USA","pickup_contact":123456798}
Now, when I try to get pickup_name
using obj.pickup_name
as done in above code, I don’t receive pickup_name value. I am still getting the whole response. I want to get values differently in different variable, for example:-
var name=obj.pickup_name;
var contact=obj.pickup_contact;
I want to get result in different variables.
3
Answers
You don’t need parse your response. You can use response.pickup_name.
You can try this:
From your controller:
Notice
success
property.In your client:
Change the dataType from
'application /json'
tojson
It will workNo need to explicitly parse to json when use dataType as json