<4a76902c3caed241.png">

Jquery ajax() method why did not get javascript text

Jquery ajax() method why did not get javascript text
0

#1

I have 2 pages. One is index.php and other is index2.php. I have some text in index.php which put by append() jquery method. In my index2.php there is empty but I included ck.js in index2.php where I called ajax() method to get index.php text specially which was create by append() method.

by ajax() method I have got all data from index.php without which I was wanted.

index.php code >

 <body>
        <div class="container-fluid">
            <div class="row">
                <h3>this is heading</h3>
                <div class="mini">
                    <ul>
                    </ul>
                </div>
            </div>
        </div>
        <script src="js/jquery-3.2.1.min.js"></script>
        <script src="js/my_script.js"></script>
    </body>

my_script.js code >

(function($){
    $(function(){
       $(".mini ul").append("<li>Bangladesh</li><li>Chittangong</li>");
    });
}(jQuery));

result is >

this is heading

Bangladesh
Chittangong

now, index2.php code >

<body>
        <script src="js/jquery-3.2.1.min.js"></script>
        <script src="js/ck.js"></script>
  </body>

ck.js code >

(function ($) {
    $(function () {
        $.ajax({
            type:"GET",
            url:"index3.php",
            beforeSend:function(){},
            success:function(html){
                    var xyz=$(html).find(".mini").html();
                    console.log(xyz);
            }
        });
    });
}(jQuery));

result >
Untitled

I have 2 questions, why

  • data not come and how can I get these ?
    Thanks all and sorry for my poor English.

  • #2

    Because there’s nothing there, it’s just a pair of <ul> tags with nothing in them. If you want list items, you need to actually have them on the page, they need to be in the html. The AJAX request just gets whatever is there, it can’t run random JavaScript on other pages, that would be a terrible idea