2011-01-19 30 views
0

我想用JavaScript方法編寫的ajax請求更新跟隨div。asp.net mvc - 如何在javascript中編寫ajax請求

asp.mvc觀點:

<div id="feed" onload="GetFeeds()"> 
      <span id="feeds" runat="server"> 
        <%= Model.Feeds %> 
      </span> 
     </div> 

我javasript做什麼?:schould

<script type="text/javascript"> 

    // some AJAX Request with 'feed' Update, but HOW?? 
    document.getElementById('feed'); 

    function GetFeeds() { 
    // need to call method from HomeModel.cs GetAllFeeds()?? 
    // or i should to write hole methode here? 
    } 

</script> 

它schould工作沒有任何控制器動作。 FeedUpdating的方法寫在HomeModel.cs/GetAllFeeds()中,並且它工作。我只需要調用它從javascript

回答

1

強烈建議使用jQuery

$.ajax( 
    url: 'http://localhost/feeds/feed1', 
    type:'GET', 
    dataType: 'json', 
    success: function(data) { 
     //Process data 
    }, 
    error: function(error) { } 
); 

它所有繁重的你和更多的瀏覽器進行測試比你將能夠在你的一生:)

2

如果你不介意,然後使用jquery $.get函數。

+0

什麼是jQuery的測試上,我可以安裝這個嗎? – 2011-01-19 10:13:17

+0

MVC是一種用jQuery構建的(他們使用jQuery,而在MVC3中使用jQuery構建Ajax的東西) – Gidon 2011-01-19 10:14:19

相關問題