Solved

JSON array

Posted on 2013-01-03
5
315 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

738 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