0

我的代码:

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);
4

1 回答 1

0

一个可能的原因可能是您ob_start("ob_tidyhandler")php.ini. 如果是,则注释此行以禁用它。

ob_tidyhandler似乎是原因。如果您检查它返回的修改后的缓冲区,它看起来与您发布的结果的标题完全相同。

希望这可以帮助。

于 2013-08-06T05:57:58.487 回答