dpus81500574 2012-09-13 03:25
浏览 22

为什么它不使用php中的ajax显示我的值

I am expecting a list of values from my php code

PHP CODE:

    $warriors = array();

foreach($result as $names){
    $warriors[] = $names;
}
echo json_encode($warriors);

JS FUNCTION:

function getWarriors() {
        $.ajax({
            url: "display_warriors.php",
            datatype:"json",
            success: function(data) {
                var toAppend = '';
                if(typeof data === "object"){
                    for(var i=0;i<data.length;i++){
                        var warrior = data[i];
                        toAppend += '<tr>';
                        toAppend += '<td>'+data[i]['warrior_id']+'</td>';
                        toAppend += '<td>'+data[i]['warrior_name']+'</td>';
                        toAppend += '<td>'+data[i]['warrior_type']+'</td>';
                        toAppend += '</tr>';
                    }
                    $("#listWarriors table").append(toAppend);
                }
            }
        });
        return false;
    }

HTML CODE TO CALL FUNCTION

<li><a href="#listWarriors" onclick="return getWarriors()">Warrior List</a></li>

When I click the a tag it does not display what I am expecting, what could be wrong

  • 写回答

1条回答 默认 最新

  • dongyuying1507 2012-09-13 03:30
    关注

    You may try to set the headers to "application/json"

    header("Content-Type: application/json");
    

    I ran into something similar and had to specify that I was sending JSON specifically. It may help.

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数