dongsashe6006 2011-01-29 17:05
浏览 95
已采纳

如何从ajax调用返回值以及HTML结果?

I've my jquery code as

 $(function() {
    $("#addline").click(function() {
        $.ajax({
            type: "POST",
            url: "proc/add-line.php",
            data: // some string,
            cache: false,
            success: function(html){
                $("ul#nextLines").append(html);

                if(lineId == 10) {                // lineId value to be passed from add-line.php
                    $("#addForm").hide();
                }
            }
        });
        }return false;
    }); 
});

In the line commented as "// lineId to be passed from add-line.php" [refer to code above], I want the php processing page add-line.php to pass the value of the var lineId.

At present, the add-line.php gives out a html code <li><?php echo $line; ?></li>. Along with that, I want to send the value of the lineId so that I can implement that conditioning.

So how should I send and then retrieve the value of the lineId (retrieved in the form of a PHP variable) from add-line.php??

Update

I've made changes to my code above as

$.ajax({
                type: "POST",
                url: "proc/add-line.php",
                data: dataString,
                dataType: 'json',
                cache: false,
                success: function(data){
                    alert(data.html);
                    $("ul#nextLines").append(data.html);
                    $("ul#nextLines li:last").fadeIn("slow");
                    $("#flash").hide();

                    if(data.lineId == 10) {
                        $("#addForm").hide();
                    }
                }

            });

And PHP code is

    // Header type
    header('Content-Type: application/json; charset=utf-8');
    $data = array(
        "html"=> "test",
        "lineId" => "1" 
    );

    echo json_encode($data);

I've not been able to retrieve the json. (even the alert(data.html) in the success function call doesn't show up).

Can you help me figure this out?? Thanks.

  • 写回答

2条回答 默认 最新

  • douying7289 2011-01-29 17:13
    关注

    One option would be to return JSON with json_encode:

    $data = array('html'=> "<li>$line</li>",
        'lineId' => $lineId //wherever that comes from
    );
    
    echo json_encode($data);
    

    JavaScript:

    $.ajax({
            type: "POST",
            url: "proc/add-line.php",
            data: // some string,
            dataType: 'json',
            cache: false,
            success: function(data){
                $("ul#nextLines").append(data.html);
    
                if(data.lineId == 10) {
                    $("#addForm").hide();
                }
            }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器