?
Solved

JSON array

Posted on 2013-01-03
5
Medium Priority
?
338 Views
Last Modified: 2013-01-03
I'm trying to get put some data into a div which is retrieved from a JSON request via ajax and jQuery:-
	$(document).ready(function () {
		$.ajax({
			type: 'GET',
			url: 'getTransactions.php?SessionID=53&AccountID=45565',
			dataType: 'json',
			success: function(retHtml, textStatus) {
			    var obj = jQuery.parseJSON(retHtml);
                $.each(obj, function(AccountID, transactionID, Date, transaction, Status, Amount, CountOfMemo) {
                    $('#divTransactions').append(AccountID + '<br/>');
                    $('#divTransactions').append(transactionID + '<br/>');
                    $('#divTransactions').append(Date + '<br/>');
                    $('#divTransactions').append(transaction + '<br/>');
                    $('#divTransactions').append(Status + '<br/>');
                    $('#divTransactions').append(Amount + '<br/>');
                    $('#divTransactions').append(CountOfMemo + '<br/><hr/>');
                    });
				},
			error: function (xhr, textStatus, errorThrown) {
				alert(xhr + "\n" + textStatus + "\n" + errorThrown);
				}
			});
    	});

Open in new window


When I look at retHtml it seems to have pulled the JSON array back, however when the debugger tries to run 'var obj = jQuery.parseJSON(retHtml);' I get
Error: Unable to get property 'length' of undefined or null reference

The page which returns the JSON request (getTransactions.php) gives:-
[
{"AccountID":"66dcf4ef-4c8b-11e2-aa23-180373676ac7","transactionID":"39528e34-4c8b-11e2-aa23-180373676ac7","Date":"2012-12-24 00:00:00","transaction":"BILL PAYMENT","Status":"Cleared","Amount":"-350.07","CountOfMemo":"0"},
{"AccountID":"66dcf4ef-4c8b-11e2-aa23-180373676ac7","transactionID":"39528e3a-4c8b-11e2-aa23-180373676ac7","Date":"2012-12-22 00:00:00","transaction":"CARD PAYMENT","Status":"Cleared","Amount":"-6.33","CountOfMemo":"0"},
{"AccountID":"66dcf4ef-4c8b-11e2-aa23-180373676ac7","transactionID":"39528e35-4c8b-11e2-aa23-180373676ac7","Date":"2012-12-22 00:00:00","transaction":"CARD PAYMENT","Status":"Cleared","Amount":"-50.97","CountOfMemo":"0"}
]

Everything looks fine, I just cant get retHtml to get into an array correctly.

Any ideas what Im doing wrong?
0
Comment
Question by:tonelm54
[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
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38741652
could you provide a link to check ?
0
 

Author Comment

by:tonelm54
ID: 38741697
Ive put the code up onto a test site:-

Test Link
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 38741729
replace :
$.each(retHtml, function(AccountID, transactionID, Date, transaction, Status, Amount, CountOfMemo) {
                    $('#divTransactions').append(AccountID + '<br/>' + transactionID + '<br/>' + Date + '<br/>' + transaction + '<br/>' + Status + '<br/>' + Amount + '<br/>' + CountOfMemo + '<br/><hr/>');

Open in new window

by :
$.each(retHtml, function(i, v) {
                    $('#divTransactions').append(v.AccountID + '<br/>' + v.transactionID + '<br/>' + v.Date + '<br/>' + v.transaction + '<br/>' + Status + '<br/>' + v.Amount + '<br/>' + v.CountOfMemo + '<br/><hr/>');

Open in new window


http://api.jquery.com/jQuery.each/
0
 

Author Comment

by:tonelm54
ID: 38741753
:-)

Thank you
0
 

Author Closing Comment

by:tonelm54
ID: 38741756
:-)

Thank you
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
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…
Suggested Courses

649 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