Solved

access data object in jquery ajax

Posted on 2014-09-08
2
159 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

786 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