Jquery returns multiple values ​​in ajax call

I have a jquery post function that returns a success response after clicking a div. However, I would like to return a few variables on success. Should I use JSON, and if so, is it possible to integrate it into the $ .ajax function after success?

$.ajax({ type: "POST", data: "action=favorite&username=" + username + "&topic_id=" + topic_id + "&token=" + token, url: "favorite.php", success: function(response) { } }); 

CHANGE I am grateful to everyone for their help + 1 to everyone!

+4
source share
5 answers

It would be very useful to use only JSON responses from the server. Thus, the server server will act as a JSON-RPC server, and the interface will be completely independent of it! Of course, you can use JSON with the $.ajax function. Here is an example:

 $.ajax({ url: 'http://some.url.com/', data: 'some=post&data=xyz', type: 'POST', dataType: 'json', success: function(response, statusText) { // `response` here is a valid JSON object; jQuery handles the work of parsing the response, etc. } }); 
+11
source

I have a jquery post function that returns a success response after clicking a div. However, I would like to return a few variables on success.

You can return only one value - a piece of text (in most cases).

However, you can structure this text so that you can easily extract different bits of data from it.

Do I need to use JSON

No, but this is the easiest option.

and if so, is it possible to integrate it into the $ .ajax function after success?

Umm. Yes. Have you read the manual for jQuery ajax function ? It explicitly mentions using JSON and getting the object as an argument to the success function.

+4
source

You will need to return JSON (or another data format supported by jQuery ajax ()) from your favorite.php file.
edit : it doesn't have to be json, but for multiple return values ​​this is easiest to parse. For example, if you returned xml or html, you would need to cross the nodes to return the value.

For example, if you returned:
{"user": "Joe", "success" : "pass", "message" : "favorite added" }

you should use:

 function(response){ var user = response.user; var success = response.success; // etc. } 

The important thing to remember is to specify the data type in your ajax call as json. jQuery also supports other data types: xml, html, script, jsonp, text

I believe that html is used by default. And by writing php to return xml with correctly formatted headers in a php script, I can tell you what you sometimes have to specify the data type for jQuery to parse it correctly.

+3
source

Say json returned is something like this

 { firstName: 'Ricardo', lastName: 'Jones', errors: 0 } 

You can use the jQuery getJSON method as follows:

 $.getJSON( 'favorite.php', { 'action': 'favorite', 'username': username, 'topic_id': topic_id, 'token': token }, function(data) { alert(data.firstName); alert(data.lastName); alert(errors); } ) 

In the return function, you can get many variables that you want.

+3
source

I have never programmed in PHP. In asp.net, JSON is the default communication mode in the async webservice call, so the developer does not need to worry about the basic details of JSON. I think even jQuery gets its data in JSON format. If in case you have several values, you can get them in the form of a list or a dictionary format.

0
source

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


All Articles