开发者

getting data as the page loads

I'm trying to get data back after an element is ready in the DOM. I'm trying to use the load function from JQUERY but I get a message .load() is not a function.

Is there a best practice when using ajax to get data for an element (in my case a div) during a page load? I开发者_如何转开发'm using ASP.NET and calling a webmethod in code behind.

Here is my ajax/jquery code:

$(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");
                  }

              });               

          });

});

Thanks.


$(document).ready() is for calling code once the DOM is ready - therefore, if I have understood you correctly, you don't need to include $("[id$=divArea]").load()(function () {

It should work like this:

$(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");
              }

          });               

      });

});

By the way - it was probably a paste error, but you also omitted the $(document).ready closing }); in the code you posted.


I think that the problem it's the code itself, try the code like this

$(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);
        })

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜