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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
newbie
  • 1,884
  • 7
  • 34
  • 55
  • Can you show your complete Ajax call? – Alex Shilman Apr 04 '14 at 04:26
  • @AlexShilman I posted the whole call. The response I posted above is from the `console.log( response );` – newbie Apr 04 '14 at 04:35
  • working fine for me http://jsfiddle.net/adiioo7/pzE7A/ – Aditya Singh Apr 04 '14 at 04: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()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) (the browser's native JSON parser) or [`$.parseJSON()`](https://api.jquery.com/jQuery.parseJSON/) (jQuery's JSON parser) to parse it into a JavaScript object that you can use. – ajp15243 Apr 04 '14 at 04:40
  • @ajp15243 its a JSON string, I used `gson` to parse it. I tried `JSON.parse()` but it removes the decimal places. Is there a way to retain the decimal places? – newbie Apr 04 '14 at 04:43
  • @newbie In JavaScript, `0.0` is going to be equivalent to `0`, since they're both just [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) objects, and those trailing `0`s are insignificant and therefore dropped during the JSON parsing. Is there any particular reason you need them? – ajp15243 Apr 04 '14 at 04:43
  • @newbie Also, note that `gson` is for converting JSON into *Java* objects, not *JavaScript* objects. – ajp15243 Apr 04 '14 at 04:47
  • @ajp15243 I'm going to display the datas in 2 decimal places. It is one of the requirements my prof said. – newbie Apr 04 '14 at 04:47
  • 1
    @newbie Rather than try to preserve insignificant trailing `0`s, 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](http://stackoverflow.com/q/6134039/1883647) (multiple times). The secret is [`Number.prototype.toFixed()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed). – ajp15243 Apr 04 '14 at 04:49

1 Answers1

1

Try this in your success function

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