0

How can I get multiple values in jquery ajax success?

My desired goal is:

$.ajax({
....
success: function(data,myvalue2) {
  dataTable.ajax.reload(); //data in here is an array that fill datatable (JSON)
  $("#input").val(myvalue2); // myvalue2 is an id for title of page
}
....
});
  • 2
    `success` -> Type: `Function( Anything data, String textStatus, jqXHR jqXHR )` - You cannot change the way `success` works (is called). You can only modify the content of `data` (_"The data returned from the server"_) – Andreas Nov 15 '21 at 11:17
  • Your `data` could be an array or json, and can contain many values. – KIKO Software Nov 15 '21 at 11:17
  • You don't. `success:` is being called *by* jquery with the `data` that is passed back from your service. Your service can't return "two results", so where would "myvalue2" come from? You can either set `myvalue2` earlier in the method and use a closure, or if both are from the server, pass it as a property. – freedomn-m Nov 15 '21 at 11:17
  • 1
    @KIKOSoftware _"Your data could be an array or json"_ -> _"JSON (JavaScript Object Notation) is a serializable data interchange format intended to be machine and human readable"_. Why list an actual data type (array) and a data format (json)? Escpecially when the array has to be converted into json to get to the client. – Andreas Nov 15 '21 at 11:22
  • my data is in json form. May I add myvalue2 to data in json mode and in success function separate them in to values (data and myvalue2) before dataTable.ajax.reload()? – user17415815 Nov 15 '21 at 11:27
  • @Andreas: I just picked two, no specific reason, I am well aware of the difference, as most people here probably are. – KIKO Software Nov 15 '21 at 11:31
  • @user17415815 if you return a "new" json with your data as one property and myvalue2 as the other property, then you can do: `success:function(data) { $("#input").val(data.myvalue2);`. As it is, your line `dataTable.ajax.reload();` isn't using `data` but you could access it with a hyperthetical `data.data` (or whatever you call your property in the server-side and whatever you call the parameter in `success:` - eg `success:function(combined) { data = combined.data; myvalue2 = combined.myvalue2; }` – freedomn-m Nov 15 '21 at 11:41

1 Answers1

0
<?php
    echo json_encode(["test1" => "value1", "test2" => "value2"]);
?>

<script>
...
success: function(data) {
    data = JSON.parse(data);
    console.log(data);
...
Ammar Aslam
  • 560
  • 2
  • 16
  • I did not get it – user17415815 Nov 15 '21 at 11:30
  • As described in comments, in ajax, you only get the data in a single variable as a string value, so the solution is to place multiple things in that string. For that, typically, JSON format is used. – Ammar Aslam Nov 15 '21 at 11:43
  • On server side, you can use a PHP array to send data in key value pairs, in my example code, you will get a JSON Object (https://www.w3schools.com/js/js_json_objects.asp). The php script will output a json string which will have the following format {test1: "value1", test2: "value2"} – Ammar Aslam Nov 15 '21 at 11:45
  • With javascript, you can read the json string and convert it into javascript object (https://www.w3schools.com/js/js_json_parse.asp) this function will take a string and make a object. So, after doing "data = JSON.parse(data)", data is replaced with a js object. After this, you can use data.test1 and data.test2 as you like... – Ammar Aslam Nov 15 '21 at 11:48
  • Read more about JSON https://stackoverflow.com/questions/4881876/why-need-to-use-json-in-php-and-ajax – Ammar Aslam Nov 15 '21 at 11:49