本文介绍了如何在页面加载时发出AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在页面加载期间调用 GetAllProperties()函数,而不是在页面后调用 GetAllProperties()函数满载。我的代码如下所示:

I need to call GetAllProperties() function during page loading instead of calling the GetAllProperties() function after page is fully loaded. My code looks like this:

<script type="text/javascript">
    $(document).ready(function () {
        GetAllProperties();
    });
    function GetAllProperties() {
        $.ajax({
            cache: false,
            url: '/Home/GetAllProperties',
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                if (response.list.length > 0) {
                    console.log(response.list)
                    var $data = $('<table id="mytable"  class="table  table-striped"> </table>');
                    var header = "<thead><tr><th>Property Name</th><th>Edit</th></tr></thead>";
                    $data.append(header);
                    $.each(response.list, function (i, row) {
                        var $row = $('<tr/>');
                        $row.append($('<td/>').html(row.PropertyName));
                        $hidden = $(' <input type="hidden" name="hid" value= "' + row.PropertyId + '">');
                        $row.append($hidden);
                        $editButton = $("<button class='editbtn' id='mybtn'>Edit</button>");
                        $row.append($editButton);
                        $deleteButton = $("<button class='deletebtn' id='delbtn'>Delete</button>");
                        $row.append($deleteButton);
                        $data.append($row);
                    });
                    $("#MyDiv").empty();
                    $("#MyDiv").append($data);
                }
                else {

                }
            },
            error: function (r) {
                alert('Error! Please try again.' + r.responseText);
                console.log(r);
            }
        });
    }
</script>

我真的需要你的帮助,因为我是jQuery的新手

I really need your help because i am new in jQuery

谢谢

推荐答案

在包含jquery之后调用你的方法:

Call your method just after including jquery:

<script src="/path/to/jquery.min.js"></script>
<script>
      function GetAllProperties() {
    $.ajax({
        cache: false,
        url: '/Home/GetAllProperties',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        success: function (response) {
            if (response.list.length > 0) {
                console.log(response.list)
                var $data = $('<table id="mytable"  class="table  table-striped"> </table>');
                var header = "<thead><tr><th>Property Name</th><th>Edit</th></tr></thead>";
                $data.append(header);
                $.each(response.list, function (i, row) {
                    var $row = $('<tr/>');
                    $row.append($('<td/>').html(row.PropertyName));
                    $hidden = $(' <input type="hidden" name="hid" value= "' + row.PropertyId + '">');
                    $row.append($hidden);
                    $editButton = $("<button class='editbtn' id='mybtn'>Edit</button>");
                    $row.append($editButton);
                    $deleteButton = $("<button class='deletebtn' id='delbtn'>Delete</button>");
                    $row.append($deleteButton);
                    $data.append($row);
                });
                $("#MyDiv").empty();
                $("#MyDiv").append($data);
            }
            else {

            }
        },
        error: function (r) {
            alert('Error! Please try again.' + r.responseText);
            console.log(r);
        }
    });
}
  GetAllProperties();
<script>

这篇关于如何在页面加载时发出AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 07:48