Solved

JSON array

Posted on 2013-01-03
5
290 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
  • 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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now