这是我的问题:
我尝试使用ajax查询从.php文件中获取内容... 数据类型在json上设置, 我用这种方式从我的php文件中回复:echo json_encode($ myData); ajax查询总是进入“错误”并给我这个:
STRINGIFY : {"readyState":4,"responseText":"\"SESSION?1\"\"<legend class=\\\"mainLegend\\\">Informations<\\/legend>CON OKUSER = 1\"","status":200,"statusText":"OK"}
这里是代码......找不到我错的地方......
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寻求帮助! :)
答案 0 :(得分:1)
echo json_encode("SESSION?".$_SESSION['user']);
echo json_encode($text);
您的回复中有两个连续的JSON文本。这不是 有效的JSON文本,因此jQuery无法将响应解析为JSON(您有dataType: "json"
因此忽略了您的(默认)text/html
内容类型)和错误
您可能需要以下内容:
header("Content-Type: application/json");
echo json_encode(Array( session => "SESSION?".$_SESSION['user'], text => $text));