Solved

access data object in jquery ajax

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now