There is an AJAX request:

 function getCourierInfo(courierId) { jQuery.ajax({ type: "GET", url:'http://domain.com/AppClient_1.0_domain/admin/courierchange.php?courier_id='+courierId, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, complete: function (courierDetails) { jQuery.ajax({ type: "POST", url: "http://mydomain.com.ua/wp-content/themes/mytheme/Controllers/CourierController.php", data: courierDetails }) } }) } 

I make a request to the application server, and then I want to send the results of this request to the server’s administrative panel (my site, to put it simply).
How can I accept, process and return this data?

  • You have two ajax. In the first you have no problems with the adoption of data. Why do you think that in the second something is done differently? - Visman
  • In the first case, I receive data on the page from which I send the request, how they are formed on the server from which I receive data - I do not know, and in the second case, I need to send data to my controller and process it. The question is exactly how to take this data, and how to form the answer. - Klimenkomud
  • Before the second jQuery.ajax({ make console.log(courierDetails); and in the browser console, see the data structure. - Visman

1 answer 1

Well, let's start with the fact that courierDetails you will actually have the entire output of the page, i.e. line. You do not process this variable anywhere, do not parse values ​​from it, do not work with it at all, but immediately stuff it into the second request in data. But in data the array of values ​​should be transferred

 jQuery.ajax({ type: "POST", url: "http://mydomain.com/muscript.php", data: { value1: '1', value2: '2', value3: '3' } }) 

But after you bring the data to this type, you can send them. And then, since yours is transmitted by the POST method, then you can get the values ​​that are transferred in the script from the $_POST array - in this example, the variables will be available as $_POST['value1'] , $_POST['value2'] , $_POST['value3'] (or from the $_REQUEST is the union of the $_GET and $_POST arrays)

  • And just send the data I received from the first request can not? Not parsing an array of data on the page from which I send requests, but parsing them already in CourierController.php - Klimenkomud
  • Can. Then you just need to send data: {input: courierDetails} , and in php to work with $_POST['input'] Although, purely from experience - I can say that it is more correct to generate output from a php file in a JSON string - then it’s enough to use JS JSON.parse(string) if you need to do something with the data, and you can send in php using JSON.stringify(object) or just a string, and in php you json_decode(string, true) use json_decode(string, true) . In general, I am in favor of data being always transferred in some standardized format - it will be easier for you in the future. - Vladimir Novopashin