Sending information back and forth using AJAX

With $ .post you can send information to the server, but what when you need to receive information from the server?

How does the information change from the way that the php variable can be held to the way that the javascript variable can hold and vice versa?

+4
source share
5 answers

This is more relevant for your question: http://docs.jquery.com/Ajax/jQuery.post

Report the results of the test.php query (HTML or XML, depending on what was returned).

$.post("test.php", function(data){ alert("Data Loaded: " + data); }); 

View the results of the test.php query with additional data payload (HTML or XML, depending on what was returned).

 $.post("test.php", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); }); 

Gets the contents of the test.php page, stores it in the XMLHttpResponse object, and applies the JavaScript function of the process ().

 $.post("test.php", { name: "John", time: "2pm" }, function(data){ process(data); }, "xml"); 

Gets the contents of the test.php page that was returned in json format ("John", "time" => "2pm")) ;? >)

 $.post("test.php", { func: "getNameAndTime" }, function(data){ alert(data.name); // John console.log(data.time); // 2pm }, "json"); 
+6
source

Check out json_encode() and json_decode() . They are now part of PHP and allow you to switch between PHP arrays and associative arrays (or stdClass objects) and javascript arrays or objects (like JSON literal).

Essentially, instead of returning xml or html, you can do echo json_encode($all_my_php_data); and return the javascript object.

If you pass 'json' as a parameter like your $ .post (), your success callback will contain a JSON object that you echoed in your PHP script.

$. post () documentation

+4
source

Then you need to get the content sent from the server. You simply define a callback function for $ .post with the data parameter. For instance:

 $.post('/index.php', { key: 'value' }, function(data) { alert(data); }); 

You can specify the return type so jQuery can automatically handle it. If you return the JSON value from a PHP script, then you must add an additional parameter at the end:

 $.post('/index.php', { key: 'value' }, function(data) { alert(data.someItem); }, 'json'); 

But if you need to get data from a PHP server without POSTing or GETting something in the first place, then you need to implement a comet. But this is a bit more work :)

0
source

The " callback " part of jQuery.post is what you want to see.

0
source

In http, you execute a request from the client (javascript in a web browser) to the server, and then process the information returned by the latter. The choice of how the information is presented in both messages is up to you.

When using AJAX (possibly through jQuery), you can make a request to the php handler, which will be responsible for returning information to the browser, usually formatted as JSON literal (you can encode the response using json_encode (), as suggested by Brian). In the end, you will analyze it on the client (for example, using jQuery) to get a javascript object.

(The function $ .post (url, [data], [callback], [type]) automatically analyzes the response and returns it to the callback function, whose signature should be the reverse (data, textStatus), where the data is the object being analyzed, and textStatus reports success or failure ( jQuery.post ).

0
source

Source: https://habr.com/ru/post/1286364/


All Articles