2011-09-08 117 views
0

我試圖在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"); 
        } 

       });    

      }); 

}); 

感謝。

+0

.load僅適用於實際加載某些內容的元素,例如iframe圖像和窗口。你究竟在做什麼? –

+0

是否可以在匹配多個元素的jQuery $對象上使用'$ object.load()'? – Blazemonger

回答

0

$(文件)。就緒()是調用代碼一旦DOM準備好 - 所以,如果我已經明白您正確,你並不需要包括$("[id$=divArea]").load()(function() {

它應該是這樣的:

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

+0

反射,你甚至不需要$(function(){});你可以直接在$(document)ready(function(){}) – lukkea

+0

這個工作中進行ajax調用!非常感謝! – MdeVera

0

我覺得現在的問題是代碼本身,嘗試像這個代碼

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

}); 
相關問題