0

---------------------------------------PHP 代码---------- ------:

    <?php

$wtype = $_POST['wtype'];

$attributes = array();

if($wtype == 'Ninja'){
    $attributes['health'] = '40-60';
    $attributes['attack'] = '60-70';
    $attributes['defense'] = '20-30';
    $attributes['speed'] = '90-100';
    $attributes['evade'] = '0.3-0.5';
}
else if($wtype == 'Samurai'){
    $attributes['health'] = '60-100';
    $attributes['attack'] = '75-80';
    $attributes['defense'] = '35-40';
    $attributes['speed'] = '60-80';
    $attributes['evade'] = '0.3-0.4';
}
else if($wtype == 'Brawler'){
    $attributes['health'] = '90-100';
    $attributes['attack'] = '65-75';
    $attributes['defense'] = '40-50';
    $attributes['speed'] = '40-65';
    $attributes['evade'] = '0.3-0.35';
}

echo json_encode($attributes);

?>

---------------------------------------HMTL 代码---------- ------:

 <div id="warriors">
        <form id="blue_team">
            <p>
                <label class="team">Blue Team</label>
            </p>
            <p>
                <a href="#Naruto"><img class="blue_img" src="resources/naruto.png" title="Ninja" alt="Naruto"></a>
                <a href="#Sasuke"><img class="blue_img" src="resources/sasuke.png" title="Samurai" alt="Sasuke"></a>
                <a href="#Sakura"><img class="blue_img" src="resources/sakura.png" title="Brawler" alt="Sakura"></a>
            </p>
            <p>
                <label>Name:</label>
                    <input id="blue_name" type="text" readonly="readonly">
                <label>Type:</label>
                    <input id="blue_type" type="text" readonly="readonly">
            </p>
        </form>

    <!--Blue Dialog-->
    <div title="Warrior Name - Blue" id="blue_form" class="blue_dialog">
        <form>
            <p>
                <label>Name:</label>
                    <input id="name" type="text">
            </p>
        </form>
        <table>

        <table>
    </div>

---------------------------------------JS 代码---------- ------:

<script>
    $(function() {
        $("button").button();
        $(".blue_img").click(function() {
            var blue_type = $(this).attr("title").trim();
            $.ajax({
                url: "get_attributes.php",
                type: "post",
                datatype: "json",
                data: {wtype: blue_type },
                success: function(data) {
                    var toAppend = '';
                    if(typeof data === "object"){
                        for(var i=0;i<data.length;i++){
                            var attributes = data[i];
                            toAppend += "<tr><td>"+data[i]['health']+"</td></tr>";
                            toAppend += "<tr><td>"+data[i]['attack']+"</td></tr>";
                            toAppend += "<tr><td>"+data[i]['defense']+"</td></tr>";
                            toAppend += "<tr><td>"+data[i]['speed']+"</td></tr>";
                            toAppend += "<tr><td>"+data[i]['evade']+"</td></tr>";
                        }
                        $("#blue_form table").append(toAppend);
                    }
                }
            });
            $("#blue_form #name").val($(this).attr("alt").trim());
            $("#blue_form").dialog({
                    width:250,
                    resizable:false,
                    modal:true,
                    buttons: {
                        "Done": function() {
                            $("#blue_name").val($("#blue_form #name").val().trim());
                            $("#blue_type").val(blue_type);
                            $("#blue_form").hide(400);
                            $(this).dialog("close");
                        }
                    }
            });
        });
    });
</script>

请我期待一个数据表输出到#blue_form,但我没有得到任何数据。但我的其他功能运行良好。但是当我看到 javascript 控制台时,它会识别方法 post

4

3 回答 3

1

您需要了解的第一件事是使用 post 方法而不是 get 没有任何效果。Post 方法相对较慢,它首先发送标头而不是数据

在 jquery ajax 块中使用此代码

<script>
    $(function() {
        $("button").button();
        $(".blue_img").click(function() {
            var blue_type = $(this).attr("title").trim();
            $.ajax({
                url: "get_attributes.php",
                type: "post",
                datatype: "json",
                data: {wtype: blue_type },
                success: function(data) {
                    var toAppend = '';
                    var newdata=$.parseJSON(data);//first decode json and get array

                            toAppend += "<tr><td>"+newdata['health']+"</td></tr>";
                            toAppend += "<tr><td>"+newdata['attack']+"</td></tr>";
                            toAppend += "<tr><td>"+newdata['defense']+"</td></tr>";
                            toAppend += "<tr><td>"+newdata['speed']+"</td></tr>";
                            toAppend += "<tr><td>"+newdata['evade']+"</td></tr>";

                        $("#blue_form table").append(toAppend);
                    }
                }
            });
            $("#blue_form #name").val($(this).attr("alt").trim());
            $("#blue_form").dialog({
                    width:250,
                    resizable:false,
                    modal:true,
                    buttons: {
                        "Done": function() {
                            $("#blue_name").val($("#blue_form #name").val().trim());
                            $("#blue_type").val(blue_type);
                            $("#blue_form").hide(400);
                            $(this).dialog("close");
                        }
                    }
            });
        });
    });
</script>

此代码将完美运行

//jsonencode 的输出不是对象而是编码字符串

于 2012-09-15T14:33:28.550 回答
1

从你的代码我不确定你为什么需要一个循环,你可以做类似的事情

success: function(data) {
    var toAppend = '';
    if(typeof data === "object") {
        toAppend += "<tr><td>"+data['health']+"</td></tr>";
        toAppend += "<tr><td>"+data['attack']+"</td></tr>";
        toAppend += "<tr><td>"+data['defense']+"</td></tr>";
        toAppend += "<tr><td>"+data['speed']+"</td></tr>";
        toAppend += "<tr><td>"+data['evade']+"</td></tr>";
        $("#blue_form table").append(toAppend);
    }
}
于 2012-09-15T14:33:47.500 回答
0
header("Content-Type: application/json");

我刚刚将此添加到我的 php.ini 文件中。感谢大家的帮助,真的很感激。tsk 只是一行代码 cud 修复

于 2012-09-15T14:40:56.327 回答