json_encode JSON.parse意外的令牌<

时间:2013-08-05 02:26:43

标签: javascript json

我的代码:

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时它会停止,并在下面给我错误信息:

  

未捕获的SyntaxError:意外的令牌&lt; 。

我尝试控制request.responseText,并获得以下结果: 更新:实际上,我意识到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"}         

似乎没有'&lt;'在我的'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);

1 个答案:

答案 0 :(得分:0)

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

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

希望这有帮助。