Solved

access data object in jquery ajax

Posted on 2014-09-08
2
161 Views
Last Modified: 2014-09-23
hi,
if i have the following jquery function getCashBackTotalAmount() that produce the object as hilited in screenshot, how can I access the TotalCashBack value?  

function getCashBackTotalAmount(u_email){
    
    console.log('cashback value');
    
    $.ajax({
        url: 'userprofile/gettotalcashback',
        data : {email:u_email},
        dataType: 'json',
        success: function(data){
            console.log('success getTotalCashBack');
            console.log(data);
            
            var $total_amt = data.TotalCashBack;
            console.log($total_amt);
            $('.earning > span').replaceWith($total_amt);
            console.log(data.length);
            
        },error : function(){
            console.log("error getTotalCashBack");
        }
        
        });
}

Open in new window

screenshot.png
0
Comment
Question by:heng tang
2 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 40310186
So this is a JSON response - there is nothing wrong with your code but I suspect it is not json that is being returned
Post the data response here.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 40310261
The error is no on this code
From the screenshot we see all the console output.
You're trying to read data outside this block

            console.log('success getTotalCashBack'); // you got it
            console.log(data); // you got it : [Object] > TotalCachback: "9.00" 
            
            var $total_amt = data.TotalCashBack;
            console.log($total_amt); // you got it : undefined
            $('.earning > span').replaceWith($total_amt);
            console.log(data.length); // you got it : 1

Open in new window


replace :
var $total_amt = data.TotalCashBack;
by :
var $total_amt = data[0].TotalCashBack;
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lazy load not working with masonry js 1 22
Downside of adding characters set in ASP pages 6 30
jQuery dialog box help 2 17
Ajax and PHP 9 31
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question