skip to Main Content

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


  1. You don’t need parse your response. You can use response.pickup_name.

    Login or Signup to reply.
  2. You can try this:

    From your controller:

    {"pickup_name":"abcd","pickup_address":"Chicago, North State Street, Chicago, IL, USA","pickup_contact":123456798, "success": true}
    

    Notice success property.

    In your client:

    if (response.success =='true'){
         alert(response.pickup_name);
    }
    
    Login or Signup to reply.
  3. Change the dataType from 'application /json' to json It will work
    No need to explicitly parse to json when use dataType as json

    $.ajax({
       type: 'GET',
       url: '/user/checkout',
       cache: false,
       data: { pickupName:pickupName,locationId:locationId },
       dataType: 'json',
       success: function (response) {
           if (response =='true'){
               alert(response.pickup_name);
    
           }
       },
       error: function (response) {
       }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search