Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JSON with Jquery?

I'm working at an app which would make a POST ajax request to a PHP script on my server. The script would query the database and return a row of records, as an array. (One array for each row, containing elements such as id, title, etc). I then want to use json_encode() to encode this array, and pass it back to the javascript which will use it to display the records.

1) How can I return the JSON encoded string to the javascript? 2) How will the javascript loop through the rows and access their fields?

like image 556
Ali Avatar asked Feb 05 '09 12:02

Ali


People also ask

How display JSON data in HTML using jQuery?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.

Which server is used while using jQuery together with JSON?

jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

Is jQuery a JSON library?

Is jQuery a JavaScript or JSON library file ? jQuery is a library of JavaScript file and it consists of DOM event effects and also the Ajax functions. jQuery is alleged to be one JavaScript file.

What is jQuery JSON and AJAX?

What About jQuery and AJAX? jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!


1 Answers

To get JSON with jQuery, just use jQuery.getJSON(). Alternatively, you can use any other AJAX tool and then just eval() the json to get a javascript object.

To loop through an array, I usually use jQuery.each():

var recordList = yourMethodToGetRecordListWithAjax();

jQuery.each(recordList, function()
{
    alert(this.Name); // For example
});
like image 92
Tamas Czinege Avatar answered Oct 04 '22 01:10

Tamas Czinege