文档就绪后,Ajax帖子无效

时间:2016-11-16 21:07:31

标签: javascript jquery ajax asp.net-mvc-4

我想在文档准备好之后进行Ajax调用。这是我的代码:

<script>

        $(window).bind("load", function () {
            getCategories();

        });
    </script>

    <script>
        function getCategories() {
            $.ajax({
                type: "POST",
                url: '@Url.Action("GetAllCategoryTest", "Category")',
                dataType: "html",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    var categoryList = JSON.parse(result);

                    $.each(categoryList.result, function (i) {
                        $("#menuCategory").append('<li>' + categoryList.result[i].CategoryName + '</li>');


                    });

                }
            });
        }
    </script>

我的ajax帖子在没有使用window.bind的情况下工作但我必须在加载文档后运行我的ajax帖子。

1 个答案:

答案 0 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    getCategories();
  });

  function getCategories() {
    $.ajax({
      type: "POST",
      url: '@Url.Action("GetAllCategoryTest", "Category")',
      dataType: "html",
      contentType: "application/json",
      async: false,
      success: function(result) {
        var categoryList = JSON.parse(result);

        $.each(categoryList.result, function(i) {
          $("#menuCategory").append('<li>' + categoryList.result[i].CategoryName + '</li>');


        });

      }
    });
  }
</script>