0

I got this response from ajax

{
  "laborCostIndex":0.0,
  "laborDailyWage":0.0,
  "laborHourlyWage":0.0,
  "laborMonthlyWage":0.0,
  "laborLeave":0.0,
  "laborBonus":0.0,
  "laborSSS":0.0,
  "laborPagIbig":0.0,
  "laborPhilHealth":0.0,
  "laborEMP":0.0,
  "laborTotalMonthlyRate":110.0,
  "laborTotalDailyRate":4.230769230769231,
  "laborTotalHourlyRate":0.5288461538461539
}

I'm trying to access the element inside through this:

response.laborCostIndex and response['laborCostIndex'] but seems doesn't work for me.

The ajax is from the xeditable here is the code:

UPDATE: posted the whole ajax

$('.laborCostIndex').editable({
    pk: '1',
    name: 'laborCostIndex',
    url: '../BTool/edit_laborCostIndex.do',
    validate: function( value ) {
        if($.trim( value ) == '') {
            return 'This field is required';
        } else if( isNaN( value ) ) {
                return 'Only accepts numbers';
        }
    },
    params: function(params) {
        var basicDailyWage = $(this).closest('td').next('td').find('a').text();
        var pagIbig = $(this).closest('tr').find(':nth-child(11)').find('a').text();
        var emp = $(this).closest('tr').find(':nth-child(13)').find('a').text();
        var datas = new Array(basicDailyWage, pagIbig, emp);

        params.pk = datas;
        return params;
    }, 
    success: function(response, newValue) {
        console.log( response );
        console.log( response.laborCostIndex );
        console.log( response['laborCostIndex'] );
    } 
});

Both results to undefined, I don't know why.

9
  • Can you show your complete Ajax call? Commented Apr 4, 2014 at 4:26
  • @AlexShilman I posted the whole call. The response I posted above is from the console.log( response );
    – newbie
    Commented Apr 4, 2014 at 4:35
  • working fine for me jsfiddle.net/adiioo7/pzE7A Commented Apr 4, 2014 at 4:37
  • Is your response parameter just a string of JSON, or has it been parsed into a legitimate JavaScript object? If it's a JSON string, then you'll want to use JSON.parse() (the browser's native JSON parser) or $.parseJSON() (jQuery's JSON parser) to parse it into a JavaScript object that you can use.
    – ajp15243
    Commented Apr 4, 2014 at 4:40
  • 1
    @newbie Rather than try to preserve insignificant trailing 0s, you can simply format your numbers when you display them to always display with 2 decimal spots. That question has been asked and answered already (multiple times). The secret is Number.prototype.toFixed().
    – ajp15243
    Commented Apr 4, 2014 at 4:49

1 Answer 1

1

Try this in your success function

 var obj = JSON.parse(response);
 console.log( obj.laborCostIndex);
2
  • This is working but the decimal places is gone. Is there a way to retain the decimal places?
    – newbie
    Commented Apr 4, 2014 at 4:40
  • try obj.laborCostIndex.toFixed(2); or obj.laborCostIndex.toFixed(1) if you want just one decimal place.
    – Senad Uka
    Commented Apr 4, 2014 at 4:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.