我的代码:
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 时它会停止,并在下面给我错误消息:
Uncaught SyntaxError: Unexpected token < 。
我尝试控制台 request.responseText,得到如下结果: update: 其实我意识到 request.responseText 不仅包含下面我想要的数据,而且还包含开头的 html 文件,我认为这是我的问题,但是我不知道如何解决它。
{"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”中似乎没有“<”。任何想法,谢谢
更新:实际上,我意识到request.responseText不仅包含我想要的数据,而且开头还包含html文件,我认为这是我的问题,但我不知道如何解决它。
<!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代码:
$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);