1

I'm making a $post call to a .php file that return json data. It works just fine if I only use one .php, but if I try including another .php it drops into the .failed callback. I've put together an example here with an included .php that doesn't even do anything.

Javascript:

$.post("/test_php/cgi.php", l_cDB_arg_array, function(o) {
  console.log("Returned from db_interface call" + JSON.stringify(o));
  console.log(JSON.stringify(o));
  l_this.showLayer("top of the world", false);
}).fail(function(response) {
  console.log('Error: ' + JSON.stringify(response));
});

First cgi.php:

<?php
include "noop.php";

header("Content-Type: application/json; charset=UTF-8");


$ret = json_decode('{"some":"data"}');

echo json_encode($ret);

Second noop.php

<?php

Result when including noop.php in cgi.php

Error: {"readyState":4,"responseText":"{\"some\":\"data\"}","status":200,"statusText":"OK"}

Result when not including noop.php in cgi.php

Returned from db_interface call{"some":"data"}

Looking at all the .fail arguments I get:

response: {"readyState":4,"responseText":"{"some":"data"}","status":200,"statusText":"OK"}

textStatus: parsererror

errorThrown: SyntaxError: Unexpected token in JSON at position 0

Moving the header to before the include didn't change anything.

  • 1
    I believe this is because you cannot output any information before you set the headers. Move your `header()` line before your `include()`, and it should work. – Obsidian Age Aug 09 '21 at 22:26
  • 3
    Skip AJAX for now, just hit `/test_php/cgi.php` in your browser, much easier to debug. I'd also see if your other file has a [BOM](https://stackoverflow.com/a/2558793/231316) – Chris Haas Aug 09 '21 at 22:31
  • The `.fail()` handler accepts 3 arguments; `(jqXHR, textStatus, errorThrown)`. The latter two are usually very handy for debugging – Phil Aug 09 '21 at 23:42
  • I thought I found the reason, but I actually had the include commented out in the previous test i ran. – Marc Henness Aug 10 '21 at 01:58
  • response: {"readyState":4,"responseText":"{\"some\":\"data\"}","status":200,"statusText":"OK"} textStatus: parsererror errorThrown: SyntaxError: Unexpected token in JSON at position 0 – Marc Henness Aug 10 '21 at 02:02
  • It's probably what @ChrisHaas mentioned about a [BOM character](https://en.wikipedia.org/wiki/Byte_order_mark). What editor are you using to author your `.php` files? – Phil Aug 10 '21 at 02:20
  • Just look at your CGI in the browser, instead of trying to guess what is going on via JS. Check your PHP/webserver logs. – Don't Panic Aug 10 '21 at 06:42
  • Thx Phil I think you are correct, but I'm thinking it is a bug in either the current version of apache, or php that is adding it. I did use stringfy to clean BOM I was seeing in some of my many attempt. I think it was when I was passing the data as application/text rather than application/json. If I get really motivated one day I'll try tracking through thee apache/php code. Till then I'll just avoid including files into my php cgi. – Marc Henness Aug 10 '21 at 17:01
  • 1
    BOMs are usually only brought in if you explicitly include them, or are using Windows default tools such as notepad, anything else and you should be good. – Chris Haas Aug 10 '21 at 23:55
  • I'm using visual studio. Also running apache on windows, which is probably not a common environment for a complex web app. I'm much more uses to IIS/.NET but wanted this app built on opensource. – Marc Henness Aug 11 '21 at 00:28

1 Answers1

0

Installed eclipse, and edited the noop.php with it. I could see one more line in the file with the eclipse editor, that was showing in visual studio. Deleting the extra line with eclipse solved the problem.