?
Solved

access data object in jquery ajax

Posted on 2014-09-08
2
Medium Priority
?
164 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 750 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 750 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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