How to redirect a json response from one php page to another?
The scenario is as follows: There are two php pages named page1.php and page2.php.onClick of a button in page3.php a ajax request is sent to page1.php. I have to redirect this request to page2.php where the processing takes places. Now I need to get the processed data in page1.php and convert it to json and send the json response to pa开发者_运维知识库ge3.php. So page1.php acts like an intermediate page for the communication between page3.php and page2.php.
If I use header("Location:page2.php") in page1.php then I cannot get the processed data from page2.php.(As I need the processed data in page1.php) Please suggest how this can be done?
Can you not just require
page2.php?
What I mean is:
page3.php -> AJAX request to page1.php
page1.php -> $request = json_decode(AJAX request from page3.php)
require page2.php
page2.php -> process $request
populate $response
page1.php -> echo json_encode($response)
page3.php -> AJAX response from page1.php
on page2.php write echo json_enode($return_data)
and retrieve on page3.php by json_decode($output,true);
and store into $_SESSION
variable and
on page3.php retrieve data by $_SESSION['']
Reference:
json_encode
json_decode
I am not sure that you can redirect an ajax request like that. Please show me otherwise if someone knows though. From what I know about these things, it sounds to me like you are using a bad way to do this.
I would suggest this as an example:
Create a class to handle your processing. The reason why, is because you may use the same thing more than once and you do not have to duplicate the code then.
You can then on page 1, require this class file like this
require_once('path/to.file');
So next, the function or piece of script that handles your ajax request on page 1 you can create a new instance of your processing class and pass the data to it. Do the processing and return the data.
Get where I am going with this? No need to redirect, just contain your processing in an object.
Here is an example
$processing = new Processing($_POST);
$return_data = $processing->run();
return json_encode($return_data);
精彩评论