通过AJAX(尤其是数组)将PHP数据更改为JS数据时的最佳实践

时间:2022-11-16 21:30:54

(Secondary title): ajax array recieved as json encoded PHP array not behaving as javascript array

(次要标题):ajax数组收到json编码的PHP数组,表现不像javascript数组

For some reason, my PHP array gets sent over to JavaScript just fine, but when I try to access an individual key of the array, it doesn't return anything, or it returns some other value which doesn't correspond. I am of course using json_encode() in the corresponding PHP file (echoClientData.php):

出于某种原因,我的PHP数组被发送到JavaScript就好了,但是当我尝试访问数组的单个键时,它不会返回任何内容,或者返回一些其他不相符的值。我当然在相应的PHP文件(echoClientData.php)中使用json_encode():

$id = $_GET['selected_id'];
    $id = str_replace("clientSel","",$id);
    $getclientinfo = "SELECT * FROM `clients` WHERE `ClientID` = $id";

    if ($result = $Database->query($getclientinfo))
    {
        $row = $result->fetch_row();
        $output = $row;
    }
echo json_encode($output);

This code works fine:

这段代码工作正常:

            $.ajax(
            {
                url: "echoClientData.php?selected_id=" + HTMLid,
                type: 'GET',
                success: function(data)
                {
                    test = data;
                    $('#client-detail-box').html(test);
                }
            });

And returns an array into the #client-detail-box div, e.g.

并将一个数组返回到#client-detail-box div,例如

["1566","","Adrian","Tiggert","","","","","","","","","","","","0000-00-00","0000-00-00","0.00","","0","","","102","Dimitri Papazov","2006-02-24","102","Dimitri Papazov","2006-02-24","1","0","0"]

However, when I do

但是,当我这样做的时候

            $.ajax(
            {
                url: "echoClientData.php?selected_id=" + HTMLid,
                type: 'GET',
                success: function(data)
                {
                    test = data[3]; // should be "Tiggert"
                    $('#client-detail-box').html(test);
                }
            });
        }

It returns

它回来了

5

2 个解决方案

#1


3  

You may need to do one of two things when returning JSON from PHP.

从PHP返回JSON时,您可能需要执行以下两种操作之一。

Either set your content type in PHP before echoing your output so that jQuery automatically parses it to a javascript object or array:

在回显输出之前,在PHP中设置内容类型,以便jQuery自动将其解析为javascript对象或数组:

header('Content-type: application/json');

or specify that jQuery should treat the returned data as json:

或者指定jQuery应该将返回的数据视为json:

$.ajax(
{
    url: "echoClientData.php?selected_id=" + HTMLid,
    type: 'GET',
    dataType: 'json',
    success: function(data)
    {
        var test = data[3]; // should be "Tiggert"
        $('#client-detail-box').html(test);
    }
});

Be aware, that in your current PHP script, in the event that your query fails, you will be json_encodeing an undefined variable.

请注意,在当前的PHP脚本中,如果查询失败,您将json_encodeing一个未定义的变量。

Also, your PHP code is entirely open to SQL injection. Make sure you sanitize your $id, either by casting it to (int) or by escaping it before sending it through in your query.

此外,您的PHP代码完全对SQL注入开放。确保通过将其转换为(int)或通过在查询中发送之前转义它来清理$ id。

#2


1  

Have you tried using JSON.parse on the value you are getting back from PHP?

您是否尝试过使用JSON.parse来获取从PHP获得的值?

e.g.

例如

$.ajax(
        {
            url: "echoClientData.php?selected_id=" + HTMLid,
            type: 'GET',
            success: function(data)
            {
                test = JSON.parse(data); // should be "Tiggert"
                $('#client-detail-box').html(test[3]);
            }
        });

That seems like it would be a fix.

这似乎是一个修复。

#1


3  

You may need to do one of two things when returning JSON from PHP.

从PHP返回JSON时,您可能需要执行以下两种操作之一。

Either set your content type in PHP before echoing your output so that jQuery automatically parses it to a javascript object or array:

在回显输出之前,在PHP中设置内容类型,以便jQuery自动将其解析为javascript对象或数组:

header('Content-type: application/json');

or specify that jQuery should treat the returned data as json:

或者指定jQuery应该将返回的数据视为json:

$.ajax(
{
    url: "echoClientData.php?selected_id=" + HTMLid,
    type: 'GET',
    dataType: 'json',
    success: function(data)
    {
        var test = data[3]; // should be "Tiggert"
        $('#client-detail-box').html(test);
    }
});

Be aware, that in your current PHP script, in the event that your query fails, you will be json_encodeing an undefined variable.

请注意,在当前的PHP脚本中,如果查询失败,您将json_encodeing一个未定义的变量。

Also, your PHP code is entirely open to SQL injection. Make sure you sanitize your $id, either by casting it to (int) or by escaping it before sending it through in your query.

此外,您的PHP代码完全对SQL注入开放。确保通过将其转换为(int)或通过在查询中发送之前转义它来清理$ id。

#2


1  

Have you tried using JSON.parse on the value you are getting back from PHP?

您是否尝试过使用JSON.parse来获取从PHP获得的值?

e.g.

例如

$.ajax(
        {
            url: "echoClientData.php?selected_id=" + HTMLid,
            type: 'GET',
            success: function(data)
            {
                test = JSON.parse(data); // should be "Tiggert"
                $('#client-detail-box').html(test[3]);
            }
        });

That seems like it would be a fix.

这似乎是一个修复。