Reputation: 2102
Here is my problem :
i try to get the content from a .php file with an ajax query... the datatype is set on json, and i give it back from my php file this way : echo json_encode($myData); the ajax query always goes into "error" and gives me this :
STRINGIFY : {"readyState":4,"responseText":"\"SESSION?1\"\"<legend class=\\\"mainLegend\\\">Informations<\\/legend>CON OKUSER = 1\"","status":200,"statusText":"OK"}
here is the code... can't find where i am wrong...
JS :
//I am already into an AJAX query, so,
//it succeeds on the first one, but the second
//one fails
...
success : function(response){
$.fancybox.close();
$("#mainField").empty();
alert('okay');
//THIS IS WHERE IT FAILS !
$.ajax({
type : 'POST',
url : './php/utils/update.php',
data : {'id':"test"},
dataType : 'json',
error : function(txt){
alert("ERROR");
alert("RESPONSE : "+txt);
alert("STRINGIFY : "+JSON.stringify(txt);
alert("RESPONSETXT : "+txt.responseText;
},
success : function(txt){
$("#mainField").html(txt);
}
});
}
...
PHP FILE (update.php) :
<?php
session_start();
include '../functions.php';
$text = '<legend class="mainLegend">Informations</legend>';
$user = $_SESSION['user'];
$db = connectToDb('test');
if($db){
$text .= "CONN OK";
}else{
$text .= "CONN FALSE";
}
$text .= "USER = ".$user;
echo json_encode("SESSION?".$_SESSION['user']);
echo json_encode($text);
?>
Thanxx for help ! :)
Upvotes: 0
Views: 499
Reputation: 944530
echo json_encode("SESSION?".$_SESSION['user']);
echo json_encode($text);
You have two, sequential JSON texts in your response. This is not a valid JSON text, so jQuery fails to parse the response as JSON (you have dataType: "json"
so it ignores your (default) text/html
content-type) and errors.
You probably want something like:
header("Content-Type: application/json");
echo json_encode(Array( session => "SESSION?".$_SESSION['user'], text => $text));
Upvotes: 1