tags:

views:

151

answers:

3

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?

A: 

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

Sanek
Thanks Sanek, will double check, might need to alter my apache MIME settings a tad
Adrian Gould
ok, I've tried that but all I get is Firefox asking to open or save the php page...
Adrian Gould
A: 

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

headers types

andres descalzo
thanks Andres, will look at that one
Adrian Gould
A: 

Set the header to application/json.

Paul
thanks Paul, will double check that... might need to alter my apache MIME settings a tad
Adrian Gould