Solved

access data object in jquery ajax

Posted on 2014-09-08
2
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
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)

707 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