繁体   English   中英

如何在ajax上获取另一页?

[英]How to get another page on ajax?

我的页面有问题,第一个代码(说的是hompe.php)

<html>
<div id="trackingkp"></div>
</html>

然后在我的ajax上,我像这样编写代码

$( document ).ready(function() {
    var dataString = '';
    $.ajax
    ({
        type: "POST",
        url: host+"ajax/tracking/kp",
        dataType: 'html',
        success: function(html)
        {
            $('#trackingkp').html($(html).find('#trackingkp').html());
            //$("#trackingkp").html($(data).find('#trackingkp').html());
        } 
    });     
});

在我的ajax控制器上,我确实喜欢这些(我正在使用laravel框架)

public function tracking($url)
{
    //var_dump("AAA");

    $this->view('ajax/tracking/'.$url,[]);
}

在我的ajax / tracking视图上

<?php
    //$json = array();
    $json = "<input type='text'></input>";
    echo json_encode($json);
?>

当我尝试显示

 Syntax error, unrecognized expression: "<input type='text'>

我已经解决了这个问题,我在不使用jQuery的情况下使用了另一个选项,因此在同一时间,在控制器上加载视图home.php时,我也尝试将加载视图也像这样

public function index(){

    is_header(); 
    $this->view('home',[]);
    $this->view('ajax/tracking/kp',[]); ---> I add these
    is_footer(); 
}

并在ajax / tracking / kp(视图)上放了这些代码

<input type='text'></input>

并且正在工作。

问题可能出在这个:

success: function(html) {
    // Problem : $(html).find('#trackingkp').html(); 
    $('#trackingkp').html($(html).find('#trackingkp').html());
}

预期此html响应将从您的给定[controller]中返回json

<?php
    //$json = array();
    $json = "<input type='text'></input>";
    echo json_encode($json);
?>

因此,这等效于:

$("<input type='text'></input>").find('#trackingkp').html();  

在这种情况下,我认为您可能不需要调用另一个* .html()。 您可以通过调用一次来简化/解决此问题:

$.ajax({
    type: "POST",
    url: host+"ajax/tracking/kp",
    dataType: 'json', // Kindly replace [html] to [json] since you are returning a [json] value
    success: function(html) {
        $('#trackingkp').html(html);        
    }
});

希望这对您有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM