Tuesday, 6 August 2013

jQuery always tries to parse the response as json

jQuery always tries to parse the response as json

I have the problem that jQuery always tries to parse my response to json.
But i don't want it. I'm expecting plain text and I recive plain text.
I want to load the content of a file into a html div. This is my JS
$.post('ajax.php',{file : file, action : 'getContent'},function(data){
if(data != 'File not found' && data != 'File could not be read')
{
$('#response').html(data);
}
else
alert(data);
},'text'); // tried to force the plain text mode
This is my ajax.php with the relvant part
else if($_POST['action'] == 'getContent')
{
unset($json); // everywhere else used for json-responses
header('Content-Type: text/plain'); // tried this to force the plain
text mode
if(file_exists(ROOT.$_POST['file']))
{
$content = file_get_contents(ROOT.$_POST['file']);
if($content !== false)
echo 'File could not be read';
else
echo $content;
}
else
echo 'File not found';
}
This is an example resonse
<?
error_reporting(-1 ^ E_NOTICE);
ini_set("display_errors",1);
$page = explode("_",$_GET['page']);
session_set_cookie_params(time()+60*60*24*30);
?>
If I execute this, I'll recieve the error: unexpected token '<' on line
550 in jquery.js. This line discribe the parseJSON function.
Why is it trying to parse it as json when I tell him, that it will get
plain text and even the header says that it is plain text?

No comments:

Post a Comment