Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在页面加载时获取数据_Asp.net_Jquery - Fatal编程技术网

Asp.net 在页面加载时获取数据

Asp.net 在页面加载时获取数据,asp.net,jquery,Asp.net,Jquery,我试图在DOM中的元素准备就绪后返回数据。我试图使用JQUERY中的load函数,但收到一条消息。load()不是函数 在页面加载期间使用ajax获取元素(在我的例子中是div)的数据时,有没有最佳实践?我正在使用ASP.NET并在代码隐藏中调用webmethod 以下是我的ajax/jquery代码: $(document).ready(function () { $(function () { $("[id$=divArea]").load()(funct

我试图在DOM中的元素准备就绪后返回数据。我试图使用JQUERY中的load函数,但收到一条消息。load()不是函数

在页面加载期间使用ajax获取元素(在我的例子中是div)的数据时,有没有最佳实践?我正在使用ASP.NET并在代码隐藏中调用webmethod

以下是我的ajax/jquery代码:

$(document).ready(function () {

      $(function () {

          $("[id$=divArea]").load()(function () {

              $.ajax({
                  type: "POST",
                  url: "apage.aspx/Role",
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  async: false,
                  success: function (response) {
                      alert("got data from Role");
                  },
                  error: function (data) {
                      alert("failed to get data from Role");
                  }

              });               

          });

});

谢谢。

我认为问题在于代码本身,请尝试这样的代码

$(document).ready(function (){

    $("[id$=divArea]").load('apage.aspx/Role',function(response, status, xhr) {
        if (status == "error") {
             var msg = "Sorry but there was an error: ";
             $("#error").html(msg + xhr.status + " " + xhr.statusText);
        })

});
$(document).ready()用于在DOM就绪后调用代码-因此,如果我理解正确,则不需要包含
$(“[id$=divArea]”)。load()(函数(){

它应该是这样工作的:

$(document).ready(function () {

  $(function () {

          $.ajax({
              type: "POST",
              url: "apage.aspx/Role",
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              async: false,
              success: function (response) {
                  alert("got data from Role");
              },
              error: function (data) {
                  alert("failed to get data from Role");
              }

          });               

      });

});

顺便说一句,这可能是一个粘贴错误,但您也省略了
$(document.ready
关闭
})
在您发布的代码中。

。load仅适用于实际加载某些内容的元素,如iframes图像和窗口。您实际上想做什么?是否可以在匹配多个元素的jQuery$对象上使用
$object.load()
?在反射时,您甚至不需要$(函数(){});围绕ajax调用的包装器-您可以直接在$(文档)ready(function(){})中进行ajax调用