Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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 mvc jquery加载返回空,可能是MVC2问题?_Asp.net Mvc_Jquery - Fatal编程技术网

Asp.net mvc jquery加载返回空,可能是MVC2问题?

Asp.net mvc jquery加载返回空,可能是MVC2问题?,asp.net-mvc,jquery,Asp.net Mvc,Jquery,我有一个网站,需要从使用asp.net MVC的不同站点获取一些数据/ 要加载的数据来自以下页面: 这应该是一个不需要动脑筋的问题,但出于某种原因,我得到了一个空洞的回答,这是我的JS: <script> jQuery.noConflict(); jQuery(document).ready(function($){ $('.totalDonations').load('http://charity.hondaclassic.com/h

我有一个网站,需要从使用asp.net MVC的不同站点获取一些数据/

要加载的数据来自以下页面:

这应该是一个不需要动脑筋的问题,但出于某种原因,我得到了一个空洞的回答,这是我的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>

jQuery.noConflict();
jQuery(文档).ready(函数($){
$('.TotalProvisions')。加载('http://charity.hondaclassic.com/home/totaldonations');
$('#charityList')。加载('http://charity.hondaclassic.com/home/CharityList');
});
在firebug中,我看到请求已发出,并返回200 OK的响应,但响应为空,如果您浏览这些页面,它们工作正常!怎么回事

以下是来自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);
        }
public ActionResult total捐赠(){
var total=“$”+repo.All().Sum(x=>x.AmountPaid.ToString();
返回内容(总计);
}
公共行动结果慈善列表(){
var charities=repo.All();
返回视图(慈善机构);
}

有人请你说出我错过了什么愚蠢的小东西-这本该花我5分钟,已经花了好几个小时了

您的total捐赠链接总共缺少o

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

$('.totalDonations').load('http://charity.hondaclassic.com/home/totaldonations');
防止通过AJAX从其他网站加载HTML。正确的方法是让方法检测请求是否来自AJAX并返回

public ActionResult(字符串回调)
{
var total=“$”+repo.All().Sum(x=>x.AmountPaid.ToString();
如果(!string.IsNullOrEmpty(回调))
{
返回内容(callback+“({total:+total+“}”);”;
}
其他的
{
返回内容(总计);
}
}
...
$.getJSON('http://charity.hondaclassic.com/home/totaldonations?callback=?',
功能(数据){
$('.total捐款').html(data.total);
});

最后,我只是在服务器端使用它,以避免上面提到的同源策略:

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

工作起来很有魅力。

当你访问charity.hondalassic.com网站时,它会抛出一个错误,必须指定“varByParam”属性才能更正我的评论,不起作用的链接是你问题中的链接,代码中的链接有效。修复了我的拼写错误,我很快就添加了输出缓存,很抱歉,我这样做是为了测试404会发生什么,但我在发布之前忘了将其更改回去,仍然返回空内容,这对我来说没有任何改变-谢谢-我只是在服务器端进行了测试。我确信同源策略是这里的问题,谢谢您的帮助
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