json_encode JSON.parse意外令牌< [英] json_encode JSON.parse Unexpected token <
问题描述
我的代码:
function callback(request, prim_key) {
var type = request.getResponseHeader("Content-Type");
if (type === "application/json") {
console.log(request.responseText);
var response = JSON.parse(request.responseText);
if (!response['success']) {
alert("Error!\n\nError number: " + response['errno'] + "\n\nError string: " + response['error']);
}
else {
alert("Update Successful!");
}
var els = getElementsByClass(prim_key);
for (var i = 0; i < els.length; i++) {
if (els[i].type != 'checkbox') {
els[i].innerHTML = response[els[i].headers];
}
}
} else {
alert("not a json reponse! check database for errors.");
}
}
当我致电jason.parser时它将停止,并在下面给我错误消息:
It will stop when i call jason.parser, and give me error message below:
未捕获到的SyntaxError:意外令牌< .
Uncaught SyntaxError: Unexpected token < .
我尝试控制台request.responseText,并获得以下结果: 更新:实际上,我意识到request.responseText不仅包含我下面想要的数据,而且开头也包含html文件,我认为这是我的问题,但我不知道如何解决.
I try to console the request.responseText, and get the result below: update: actually, I realize request.responseText not just contain the data I want below, but also contain the html file in the beginning,I think that's my problem, but I don't know how to solve it.
{"success":true,"ID":"131","Manufacturing_ID":"88888","Sequence_Name":"NDU","Bases":"22","Sequence":"TGG AGA TGT GTG CGT TCT TCTGG AGA TGT GTG CGT TCT TCTGG AGA TGT GTG CGT TCT TC","Tm_C":"55.00000000","nmoles":"26.0","Owner":"Liang","Species":"gallus"}
似乎没有<"在我的"resonseText"中.任何想法,谢谢
it seems there is not '<' in my 'resonseText'.any ideas, thanks
更新:实际上,我意识到request.responseText不仅包含下面想要的数据,而且开头也包含html文件,我认为这是我的问题,但我不知道如何解决.
update: actually, I realize request.responseText not just contain the data I want below, but also contain the html file in the beginning,I think that's my problem, but I don't know how to solve it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...................
{"success":true,"ID":"131","Manufacturing_ID":"88888","Sequence_Name":"NDU","Bases":"22","Sequence":"TGG AGA TGT GTG CGT TCT TCTGG AGA TGT GTG CGT TCT TCTGG AGA TGT GTG CGT TCT TC","Tm_C":"55.00000000","nmoles":"26.0","Owner":"Liang","Species":"gallus"}
我的php代码:
my php code:
$query = "SELECT * FROM $table WHERE ";
$i = 0;
foreach ($pks as $pk) {
$query .= "$pk='$pvs[$i]' AND ";
$i++;
}
$query = rtrim($query, " AND ");
$result = $db_conn->query($query);
$row = $result->fetch_assoc();
//$flag_key='';
if ($db_conn->errno) {
echo "oops";
} else {
foreach ($row as $key => $value) {
$response[$key] = $value;
//$flag_key .=$key.'-'.$value.'***';
}
}
header('Content-Type: application/json');
echo json_encode($response);
推荐答案
一个可能的原因可能是您在php.ini
中启用了ob_start("ob_tidyhandler")
.如果是,则注释此行以将其禁用.
One possible reason could be you have ob_start("ob_tidyhandler")
enabled in php.ini
. If yes, then comment this line to disable it.
ob_tidyhandler 可能是原因.如果您检查修改后的缓冲区,它将返回,看起来与您发布的结果的标题完全相同.
ob_tidyhandler seems to be the cause. If you check the modified buffer it returns, which looks exact same as the header of the result you posted.
希望这会有所帮助.
这篇关于json_encode JSON.parse意外令牌<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!