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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?