On 13/02/2023 03:35, John Iliffe wrote:
On Sun, 2023-02-12 at 22:41 +0000, Ashley Sheridan wrote:On 12/02/2023 22:29, John Iliffe wrote:On Sun, 2023-02-12 at 21:15 +0000, Ashley Sheridan wrote:On 12/02/2023 19:49, John Iliffe wrote:I seem to be getting extraneous data in the response sent to the browser by PHP. The server query is an AJAX request in json format which is received and processed correctly and the result is sent using the "echo" function. I have checked and the area where the response is constructed is empty when processing starts. The data received by the browser has BOTH the request json data followed by the correct response in the received string (result not HTML, just a string that will eventually be converted to json but so far will only be caught in the browser for display). I'm using XMLHttpObject in the browser and I haven't been able to find a way to separate the request from the response. Before I construct an elegant workaround does POST normally return the request? If not, what am I doing wrong? So far I have always used GET so I know it doesn't return the request. For completeness, here is what the server says it is sending to the browser (using echo) (The json may be wrong; I'm still just trying to receive it). ------ sent to browser: "jres":[{"item_id":"5","i_type":"OL","i_name":"Student Success Pages","i_stock":"99924","i_price":"14.00","i_books_box":"0"},{"item_id":"13","i _type":"CD","i_name":"Basic Instructors Guide 9th Ed.","i_stock":"9983","i_price":"19.95","i_books_box":"0"},{"item_id":"20","i_ty pe":"SG","i_name":"Canadian Amateur Radio Basic Qualification Study Guide","i_stock":"274","i_price":"44.95","i_books_box":"20"},{"item_id":"21","i_ type":"SG","i_name":"Canadian Amateur Radio Advanced Qualification Study Guide","i_stock":"1586","i_price":"49.95","i_books_box":"16"}] ----- here is the original request from the browser, as decoded by server ------ request method: POST requests: array ( 'req' => '{"func":"act","item":"438"}', ) json last error message: No error decoded_args: func=act item=438 ------- and here is what the browser says it received back: ----- received:-->array(2) { ["func"]=> string(3) "act" ["item"]=> string(3) "438" } "jres":[{"item_id":"5","i_type":"OL","i_name":"Student Success Pages","i_stock":"99924","i_price":"14.00","i_books_box":"0"},{"item_id":"13","i _type":"CD","i_name":"Basic Instructors Guide 9th Ed.","i_stock":"9983","i_price":"19.95","i_books_box":"0"},{"item_id":"20","i_ty pe":"SG","i_name":"Canadian Amateur Radio Basic Qualification Study Guide","i_stock":"274","i_price":"44.95","i_books_box":"20"},{"item_id":"21","i_ type":"SG","i_name":"Canadian Amateur Radio Advanced Qualification Study Guide","i_stock":"1586","i_price":"49.95","i_books_box":"16"}]<-- ----- Thanks in advance for any suggestions you can give. John ======Hi John, What does your PHP code look like? Something in it must be outputting the request, as that's not something that would just occur all by itself. Regards, AshThanks for the quick response Ash. That's what I have been asking myself and I wondered if returning the query is a requirement of the POST protocol. Without sending the entire script, I can say that there are only two "echo" statements in it (by doing a grep for echo) and they are mutually exclusive: ----- [John@prod04 test]$ grep -n echo ./XXXXXXb.php 140: echo $json_resp; 145: echo "<br />XXXXXb: Either func or item was not set<br />err is true, errno=" . $errno . "<br />"; ----- <?php ob_start(); /* XXXXXXb responds to AJAX inventory report requests * from updated XXXXXXXX. * * Written J Iliffe 2/2023 * copyright John Iliffe, 2023, all rights reserved. */ //header('Content-Type: application/json; charset=utf-8'); header('Content-Type: text/plain; charset=utf-8'); $func = ""; $item = 0; $err = false; $errno = 0; $fx = fopen("/tmp/XXXXXb.txt","w"); fwrite($fx,("\nrequest method: " . $_SERVER['REQUEST_METHOD'])); fwrite($fx,("\nrequests: " . var_export($_REQUEST,true))); /* get the json request block as associative array */ if (isset($_POST['req'])) { $decoded_args = json_decode($_POST['req'],true,5,JSON_INVALID_UTF8_IGNORE); if ((isset($decoded_args['func'])) && (isset($decoded_args['item']))) { $func = $decoded_args['func']; $item = $decoded_args['item']; fwrite($fx,("\nfunc and item isset as " . $func . " and " . $item . "\n")); } else { $err = true; $errno = 2; } fwrite($fx,("\njson last error message: " . json_last_error_msg() . "\n")); fwrite($fx,("decoded_args:" . var_dump($decoded_args) . "\n")); fwrite($fx,("func=" . $decoded_args['func'] . "\n")); fwrite($fx,("item=" . $decoded_args['item'] . "\n")); } else { $err = true; $errno = 1; } /* if we are good to go here, open database, create query */ $json_resp = '"jres":['; //json opening ----I deleted the database activity from here--- if (! $err) { if (! $res) { $err=true; $errno = 4; } else { for ($i=0;$i<$rows;$i++) { $r_arr = pg_fetch_array($res,null,PGSQL_ASSOC); $json_rec=json_encode($r_arr,0,16); if ($json_rec === false) { fwrite ($fx,("\nrecord at " . $i . " bad, " . json_last_error_msg() . "\n")); } if (($i < $rows) && ($i != 0)) // no opening or trailing ',' { $json_resp .= ','; } $json_resp .= $json_rec; } $json_resp .= ']'; fwrite($fx,("\njson sent to browser:\n -->" . $json_resp . "<---\n\n\n")); } } } if (! $err) { echo $json_resp; ob_flush(); } else { echo "<br />adm9005b: Either func or item was not set<br />err is true, errno=" . $errno . "<br />"; ob_flush(); } exit; ?> ------ This is pretty much what I have so far and why it has been driving me nuts for several days now...the query is never repeated anywhere in the echoed output. What actually comes back to the browser, as you can see above, is is the result generated by this; prepended with PHP's analysis of the original POST query. Thanks for taking the time to look. John ======It might be some error output, which is being supressed by the ob_start(), and then output when you do the ob_flush(). Why are you using those, as it's unusual for what you appear to be doing here. Thanks, AshThanks for the suggestion Ash. I commented out the ob_start and ob_flush statements but the problem persists. I was using buffering because I have to send headers as part of the response, at the very least the content-type header when I convert the response to json. The reason it is a simple string at the moment is that I didn't want my inexperience with json to get involved with getting the communications working properly. Regards, John ======
Have you tried attaching a debugger, to see at what point the extra output is being shown? Is it being added to the response variable somewhere, possibly from the code that you removed from your excerpt?
Also, you don't need to use object buffers if you're sending headers. Typically, you'd use output buffering if you wanted to prevent something from making it to the final output, or if you needed to control the output during a streamed response.
Thanks, Ash -- www.ashleysheridan.co.uk