2012-02-10 89 views
0

我尝试使用AJAX加载文件,该文件无效。ASP.NET MVC AJAX加载文件

查看

@{ 
    ViewBag.Title = "Index"; 
    Layout = null; 
} 

<html> 
<head> 
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("button").click(function() { 
      $("div").load('test1.txt'); 
     }); 
    }); 
</script> 
</head> 
<body> 

<div><h2>Let AJAX change this text</h2></div> 
<button>Change Content</button> 

</body> 
</html> 

到test1.txt的路径是: 〜/查看/的Ajax/test1.txt的

到视图的路径是 〜/查看/的Ajax/Index.cshtml

有什么想法?

回答

0

将您的文件移动到~/Content/test1.txt。内容路径不受MVC路由规则的影响。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("button").click(function() { 
      $("div").load('@Url.Content("~/Content/test1.txt")'); 
     }); 
    }); 
</script> 
+0

谢谢您的建议。有用!! – Pingpong 2012-02-10 21:57:33