2009-11-30 67 views
0

我有需要從不同的靜坐一些數據,使用asp.net MVC/jquery load返回空,可能MVC 2的問題?

的數據得到加載從這些頁面的網站:
http://charity.hondaclassic.com/home/totaldonations
http://charity.hondaclassic.com/Home/CharityList

這應該是沒有道理的,但由於某種原因,我得到一個空的響應,這是我的JS:

<script> 
    jQuery.noConflict(); 
    jQuery(document).ready(function($){ 
      $('.totalDonations').load('http://charity.hondaclassic.com/home/totaldonations'); 
     $('#charityList').load('http://charity.hondaclassic.com/home/CharityList'); 
    }); 

</script> 

在Firebug我看到的請求,而且用200 OK b A響應回來如果你瀏覽這些網頁,他們的工作很好!有沒有搞錯?

下面是從MVC站點控制器操作:

public ActionResult TotalDonations() { 
      var total = "$" + repo.All<Customer>().Sum(x => x.AmountPaid).ToString(); 
      return Content(total); 
     } 

     public ActionResult CharityList() { 
      var charities = repo.All<Company>(); 
      return View(charities); 
     } 

有人請了什麼,我缺少的愚蠢的小事情 - 這應該採取我5分鐘,它一直小時!

+0

當你進入charity.hondaclassic.com網站時,它會拋出一個錯誤,*必須指定'varByParam'屬性* – 2009-11-30 21:18:52

+0

來糾正我的評論,不起作用的鏈接是你問題中的鏈接,鏈接在你的代碼裏面工作。 – 2009-11-30 21:19:56

+0

解決了我的拼寫錯誤,我在輸入緩存中添加了真正的快速,對此感到抱歉 – Slee 2009-11-30 21:47:12

回答

2

same origin policy防止加載HTML從通過AJAX另一個網站。做到這一點的正確方法是讓方法檢測請求是否來自AJAX,並返回JSONP

public ActionResult TotalDonations(string callback) 
{ 
    var total = "$" + repo.All<Customer>().Sum(x => x.AmountPaid).ToString(); 
    if (!string.IsNullOrEmpty(callback)) 
    { 
     return Content(callback + "({ total: " + total + " });"); 
    } 
    else 
    { 
     return Content(total); 
    } 
} 

... 
$.getJSON('http://charity.hondaclassic.com/home/totaldonations?callback=?', 
      function(data) { 
       $('.totalDonations').html(data.total); 
      }); 
+0

,這並沒有改變我的一件事 - 謝謝 - 雖然我只是做了服務器端 – Slee 2009-11-30 22:02:02

+0

我相信相同的原產地政策是這裏的問題,感謝您的幫助 – Slee 2009-11-30 22:04:04

0

您totaldonations鏈接缺少Ø

> $('.totalDonations').load('http://charity.hondaclassic.com/home/ttaldonations'); 

應該

$('.totalDonations').load('http://charity.hondaclassic.com/home/totaldonations'); 
+0

好的皮卡@jmein – griegs 2009-11-30 21:32:10

+0

對不起,我測試了一下404會發生什麼,我忘了在發佈前忘記更改它,仍然返回空內容 – Slee 2009-11-30 21:48:35

0

最後我只是做服務器端,以避免上述同源策略:

Dim totalDonations As String 
    Dim charities As String 

    Using Client As New System.Net.WebClient() 
     totalDonations = Client.DownloadString("http://charity.hondaclassic.com/home/totaldonations") 
     charities = Client.DownloadString("http://charity.hondaclassic.com/home/CharityList") 
    End Using 

工作就像一個魅力。