0

I recently started translating an old Classic ASP site to PHP. Several of its pages (Response.ContentType = "application/json") would just serve JSON responses such as {"R":1} and everything worked fine.

now on PHP, with header("content-type:application/json") first thing on the code, ajax just will not parse it. The client-side JS code is the same I used before. I haven't even touched it.

$.ajax({
   dataType: "json",
   type : "POST",
   url: "processthisrequest.php",
   cache: false,
   async: false,
   data: { Field1:"bla", Field2:"blabla"},
   error: function(data){
      // code on error
   },
   success: function(json){
      // code on success
   }
});

if the request is accepted ALL it serves is {"R":1} with double-quotes as it's always been.

Ajax will fire the error function no matter what. trying to debug it I found this:

readyState:4
responseText:{"R":1}
status:200
statusText:OK

searching for help I found a lot of people with problems with ajax getting readyState:4, status:200 and the response still not being parsed. none of the solutions worked or applied to my problem.

as it was working with IIS/ASP, can it be something with Apache or PHP?

UPDATE: still no success, but if I get the server to serve a .js file with {"R":1} instead of processing a response through a php page ajax will fire the success function. which only proves my point that this is a php problem and also explains why it worked with old dinosaur ASP. now why PHP is not serving a proper mime-type is something I am trying to figure out.

A. Richards
  • 123
  • 1
  • 9
  • What do you get in the console if you add `console.log(data);` to the error function? – AlliterativeAlice May 02 '15 at 21:54
  • readyState:4, responseText:"{"R":1}", status:200, statusText:"OK" and functions to all the other properties. – A. Richards May 02 '15 at 21:59
  • Try changing the `dataType` parameter to `text` and see if success is called. – AlliterativeAlice May 02 '15 at 22:03
  • with `dataType: "text"` success is called, however php gives me several errors. the lines that would come after `header` would collect the data from the form, one by one. now it's giving me the same error this guy was having: http://stackoverflow.com/questions/26261001/warning-about-http-raw-post-data-being-deprecated – A. Richards May 02 '15 at 22:18
  • It sounds like the error is happening because php is returning warning messages as part of the response, causing it to not be valid JSON. – AlliterativeAlice May 02 '15 at 22:22
  • yes, but not entirely because of that. just out of curiosity I changed the setting in php.ini, restarted the server and it just seemed as if it was ignoring or not receiving the parameters. in Firebug the parameters section of the request disappeared and the error PHP was throwing was because the code was expecting $_POST data it did not receive. I reverted it back to the original setting and I'm now back at the original problem. – A. Richards May 02 '15 at 22:39
  • PHP is not throwing any more errors, with `dataType:"text"` it fires the success function but it's not parsing the response. even after I got creative and tried to parse it with `jQuery.parseJSON` it still won't work... – A. Richards May 02 '15 at 22:49
  • And if you log the response to console? – AlliterativeAlice May 02 '15 at 22:53
  • it will show the response `{"R":1}` and nothing else. `jQuery.parseJSON` refuses to parse it saying it has an invalid character but it won't tell me which one. – A. Richards May 02 '15 at 23:00
  • Try logging the request/response body and see what the response body is. – AlliterativeAlice May 02 '15 at 23:01
  • I did... that's all I'm getting. that's all the server is sending: `{"R":1}`. I am trying a couple things and if there's no success I'll try a different PHP version. currently I'm using thread-safe x64. – A. Richards May 02 '15 at 23:11
  • 1
    I mean log it using fiddler or your browser's network logging, not `console.log` – AlliterativeAlice May 02 '15 at 23:14
  • I did... I used Firebug and Firefox's network logs. all the server sends back is `{"R":1}` and the response headers: `Connection: "Keep-Alive"`, `Content-Length: "20"`, `Content-Type: "application/json; charset=UTF-8"`, `Date: "..."`, `Keep-Alive: "timeout=5, max=100"`, `Server: "Apache/2.4.12 (Win64)"`. Request Headers are `Host`, `User-Agent`, `Accept: "application/json, text/javascript, */*; q=0.01"`, `Accept-Language`, `Accept-Encoding`, `DNT:1`, `X-Requested-With:"XMLHttpRequest"` and the usual ones. I honestly don't know what else I can try... – A. Richards May 03 '15 at 01:07
  • The `Content-Length: "20"` is a bit too much for string with length 7 bytes. Are you sure, that you are not sending something else to the server? Try removing the closing tag (`?>`). – VolenD May 03 '15 at 08:01
  • @user3584460: I don't really get your suggestion to remove closing tag. I'm pretty sure processing of the page will crash. the page doesn't have any HTML to send. it literally only processes the data received and sends a json formatted yes or no response. the 20 characters the browser is sending to the server is URL-encoded non-english accented letters. – A. Richards May 03 '15 at 10:14
  • You can check the following question: http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag. And the 20 characters are the response headers, not the request headers, according to you comment. The problem is, if you have spaces or newlines after the PHP closing tag it may break the response JSON. Removing the closing tag solves this problem. – VolenD May 03 '15 at 12:03
  • 1
    @user3584460 I sorted it. posted a response. – A. Richards May 03 '15 at 12:14

1 Answers1

0

I sorted it.

PHP has some very strange ways of doing things. right after the header("Content-Type: application/json") line I have an include "file.php". although both the file that processes the Ajax request and the include file were saved as Unicode one had BOM signature and the other hadn't.

processing these two files made PHP send "something" back to the browser that was anything but a proper application/json response which wouldn't allow Ajax to parse it.

I know I'm supposed to stay on point here but I am so frustrated I need to add this comment: after wasting almost 18 hours of my life on this, time I could have used working on other things, the only thing I can learn from this experience is that free software is not free at all.

A. Richards
  • 123
  • 1
  • 9