【问题标题】:json_encode doesnt respond for getJsonjson_encode 对 getJson 没有响应
【发布时间】:2012-12-29 05:23:14
【问题描述】:

我有一个 html 页面,它会发送 json 请求以从服务器获取数据 [通过 php-mysql]。我看到没有收到回复。我尝试通过 fiddler 进行调试,可以看到“响应不包含有效的 json 文本。

我的代码在下面

html

<!DOCTYPE html>
<html>
<script src="http://code.jquery.com/jquery-latest.js" 
        type="text/javascript">
</script>

<script type="text/javascript">
function populateFruitVariety() {

    $.getJSON('serverside.php', 
               {fruitName:$('#fruitName').val()}, 
               function(data)  {

    var select = $('#fruitVariety');

    if(select.prop) {
        var options = select.attr('options');
    } else {
        var options = select.attr('options');
    }

    $('option', select).remove();

    $.each(data, function(val, text) {
        options[options.length] = new Option(text, val);
    });
    });
}

$(document).ready(function() {
    populateFruitVariety();
    $('#fruitName').change(function() {
        populateFruitVariety();
    });
});

</script>

<form>
    Fruit:
    <select name="name" id="fruitName">
        <option>Apple</option>
        <option>Banana</option>
        <option>Orange</option>
        <option>Pear</option>
    </select>
    Variety:
    <select name="variety" id="fruitVariety">
    </select>
</form>
</html>

服务器端.php

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

$host="localhost";
$username="root";
$password="";
$database="db_common";
$table_fruit_info = "fruit_info";
$tc_fruit_id = "fruit_id";
$tc_fruit_index = 'fruit_index';

$con = mysql_connect("$host", "$username", "$password") 
       or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

if(isset($_GET['fruitName'])) {
    $fruit_id = $_GET['fruitName'];
    $result = mysql_query("SELECT * FROM $table_fruit_info
                WHERE $tc_fruit_id='$fruit_id'");
    while($rows = mysql_fetch_array($result))
    {
        $ret_fruitIndex = $rows[$tc_fruit_index];
    }
}
echo json_encode($ret_fruitIndex);
?>

感谢您的帮助!

【问题讨论】:

标签: php jquery ajax json getjson


【解决方案1】:

看起来$ret_fruitIndex 只是一个字符串,它没有适当的 JSON 代表。在您的 while 循环中,您每次通过都会覆盖此变量的值,我认为这不是您想要做的。

我假设你真的想要这样的东西:

$ret_fruitIndex[] = $rows[$tc_fruit_index];

另一方面,您的代码很容易受到 SQL 注入的影响,您也不应该使用已弃用的 mysql_* 函数。我建议使用mysqli_* 函数、PDO 或更现代、更安全的东西。

关于你的 jQuery:我不确定你在这里做什么:

if(select.prop) {
    var options = select.attr('options');
} else {
    var options = select.attr('options');
}

因为那里的情况没有任何区别。

我也相信你的each() 函数是错误的。传递给回调函数的参数将是数据的索引值和该索引的值。

您的逻辑应该是删除所有选项,而不是根据数据添加新选项,所以我不知道为什么options 变量与此回调函数有任何关系。

【讨论】:

  • 谢谢一百万。我做了 $ret_fruitIndex[] 并且可以看到 json 响应来自 html 页面 [通过警报(数据)验证]。但是选择选项不会填充返回的数据。我的 javascript/jquery 有什么问题吗?
【解决方案2】:

你需要一个水果名称数组,所以替换 $ret_fruitIndex = $rows[$tc_fruit_index];

与 $ret_fruitIndex[] = $rows[$tc_fruit_index];

【讨论】:

  • 感谢joyce,我创建了$ret_fruitIndex[] 并且可以看到json 响应来到了html 页面[通过警报(数据)验证]。但是选择选项不会填充返回的数据。你看到我的 javascript/jquery 有什么问题吗?
【解决方案3】:

修复 PHP 后,将 JS 回调更改为:

function(data)  {

    var select = $('#fruitVariety');

    select.empty();

    $.each(data, function(val, text) {
        select.append($('option', {value: val}).text(text));
    });

});

options 不是对 DOM 中数组的引用,它是一个 jQuery 对象,因此添加到它不会更新 DOM。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多