0

I am trying to debug a simple PHP/JSON/jQuery problem.

The following PHP script:

header('Content-type: text/javascript');
echo json_encode(array('type'=>'error','message'=>'arg')); 

is being consumed by jQuery but when the line:

var results = jQuery.parseJSON(responseText);

is executed, the jQuery JSON parser gives the following:

uncaught exception: Invalid JSON: <head></head><body><pre>{"type":"error","message":"oops!"}</pre></body>

Obviously the head/body/pre are not supposed to be returned.

I cannot see any hidden characters nor anything out of order in my PHP code..

Any ideas?

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Is this the exact response sent from the server when looking with FireBug? Could the server you are hosting this script to appends those HTML tags? – Darin Dimitrov Sep 11 '10 at 10:02
  • Do you use any PHP framework? – Felix Kling Sep 11 '10 at 11:41
  • Darin - I'd have to check again - I'll do a SMALL PHP script and see what happens. keep an eye open for an update. Felix - no I am not, mainly because this application is literally a one page set-up to solve a problem for myself and my colleagues. I've been naughty and even kept it non-OOP, as it should be a simple problem. – Adrian Gould Sep 12 '10 at 06:53
  • Ok - when i use a small PHP script: 'success','message'=>'quicktest'); echo json_encode($output); ?> the result is to get a request to open or save the "file"... hmmm – Adrian Gould Sep 12 '10 at 07:20
  • setting Content-Type: text/plain; seems to alleviate the problem a bit. – Adrian Gould Sep 12 '10 at 07:46
  • Try opening it - if all is okay you should see the json text. – Paul Sep 12 '10 at 10:27

4 Answers4

1

This one has had me stumped for the last two days. I'm using the jQuery Form plugin's ajaxSubmit function to submit a form via AJAX without reloading the page. I finally stumbled across the answer after this question showed me a parameter I hadn't noticed previously: dataType.

Behind the scenes, an iframe is being created and is actually making the call back to the server. The response from the server is being pulled from the iframe, which is bringing along with it the tags.

The jQuery Form plugin handles the situation by allowing you to specify the type of response to expect from the server. If I specify 'json' as the response type, the following few lines of code are executed to get the JSON from within the tags:

// account for browsers injecting pre around json response
var pre = doc.getElementsByTagName('pre')[0];
if (pre) {
    xhr.responseText = pre.innerHTML;
}

(doc is a reference to the iframe's document and xhr is the XmlHttpResponse object that ultimately gets returned from the plugin's function.)

I don't know exactly how you're making your AJAX call, but I'm guessing a similar construct (perhaps using a document fragment) will allow you to extract the necessary JSON from the response.

Community
  • 1
  • 1
JSmitty
  • 248
  • 1
  • 7
0

Try not to send header('Content-type: text/javascript');

0

json for php find "function send_as_json($obj)"

headers types

andres descalzo
  • 14,887
  • 13
  • 64
  • 115
0

Set the header to application/json.

Paul
  • 1,122
  • 1
  • 10
  • 18