jquery ajax调用总是转到错误部分,但返回的数据是正常的

时间:2022-10-08 09:02:27

Here is my problem :

这是我的问题:

i try to get the content from a .php file with an ajax query... the datatype is set on json, and i give it back from my php file this way : echo json_encode($myData); the ajax query always goes into "error" and gives me this :

我试图从带有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"}

here is the code... can't find where i am wrong...

这是代码......找不到我错的地方......

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文件(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 for help ! :)

Thanxx寻求帮助! :)

1 个解决方案

#1


1  

echo json_encode("SESSION?".$_SESSION['user']);
echo json_encode($text);

You have two, sequential JSON texts in your response. This is not a valid JSON text, so jQuery fails to parse the response as JSON (you have dataType: "json" so it ignores your (default) text/html content-type) and errors.

您的响应中有两个连续的JSON文本。这不是有效的JSON文本,因此jQuery无法将响应解析为JSON(您有dataType:“json”,因此它忽略了您的(默认)text / html内容类型)和错误。

You probably want something like:

你可能想要这样的东西:

header("Content-Type: application/json");
echo json_encode(Array( session => "SESSION?".$_SESSION['user'], text => $text));

#1


1  

echo json_encode("SESSION?".$_SESSION['user']);
echo json_encode($text);

You have two, sequential JSON texts in your response. This is not a valid JSON text, so jQuery fails to parse the response as JSON (you have dataType: "json" so it ignores your (default) text/html content-type) and errors.

您的响应中有两个连续的JSON文本。这不是有效的JSON文本,因此jQuery无法将响应解析为JSON(您有dataType:“json”,因此它忽略了您的(默认)text / html内容类型)和错误。

You probably want something like:

你可能想要这样的东西:

header("Content-Type: application/json");
echo json_encode(Array( session => "SESSION?".$_SESSION['user'], text => $text));