【发布时间】:2011-02-19 15:03:09
【问题描述】:
这是previous问题的延续。
现在我正在尝试调用我在 ASP.NET MVC 应用程序中定义的启用 AJAX 的 Web 服务(即MovieService.svc)。但是我的getMovies javascript 函数中从未调用过该服务。
如果我在非 ASP.NET MVC 应用程序中尝试这种调用 AJAX Web 服务的相同技术,它可以正常工作,所以我想知道当它尝试创建 AJAX 时,ASP MVC 路由是否会以某种方式干扰事物网络服务调用。
你知道为什么我的网络服务没有被调用吗?代码如下。
<script src="<%= ResolveClientUrl("~/scripts/jquery-1.4.2.min.js") %>" type="text/javascript"></script>
<script src="<%= ResolveClientUrl("~/scripts/grid.locale-en.js") %>" type="text/javascript"></script>
<script src="<%= ResolveClientUrl("~/scripts/jquery-ui-1.8.1.custom.min.js") %>"
type="text/javascript"></script>
<script src="<%= ResolveClientUrl("~/scripts/jquery.jqGrid.min.js") %>" type="text/javascript"></script>
<script type="text/javascript">
var lastsel2;
function successFunction(jsondata) {
debugger
var thegrid = jQuery("#editgrid");
for (var i = 0; i < jsondata.d.length; i++) {
thegrid.addRowData(i + 1, jsondata.d[i]);
}
}
function getMovies() {
debugger
// ***** the MovieService#GetMovies method never gets called
$.ajax({
url: 'MovieService.svc/GetMovies',
data: "{}", // For empty input data use "{}",
dataType: "json",
type: "GET",
contentType: "application/json; charset=utf-8",
success: successFunction
});
}
jQuery(document).ready(function() {
jQuery("#editgrid").jqGrid({
datatype: getMovies,
colNames: ['id', 'Movie Name', 'Directed By', 'Release Date', 'IMDB Rating', 'Plot', 'ImageURL'],
colModel: [
{ name: 'id', index: 'Id', width: 55, sortable: false, hidden: true, editable: false, editoptions: { readonly: true, size: 10} },
{ name: 'Movie Name', index: 'Name', width: 250, editable: true, editoptions: { size: 10} },
{ name: 'Directed By', index: 'Director', width: 250, align: 'right', editable: true, editoptions: { size: 10} },
{ name: 'Release Date', index: 'ReleaseDate', width: 100, align: 'right', editable: true, editoptions: { size: 10} },
{ name: 'IMDB Rating', index: 'IMDBUserRating', width: 100, align: 'right', editable: true, editoptions: { size: 10} },
{ name: 'Plot', index: 'Plot', width: 150, hidden: false, editable: true, editoptions: { size: 30} },
{ name: 'ImageURL', index: 'ImageURL', width: 55, hidden: true, editable: false, editoptions: { readonly: true, size: 10} }
],
pager: jQuery('#pager'),
rowNum: 5,
rowList: [5, 10, 20],
sortname: 'id',
sortorder: "desc",
height: '100%',
width: '100%',
viewrecords: true,
imgpath: '/Content/jqGridCss/redmond/images',
caption: 'Movies from 2008',
editurl: '/Home/EditMovieData/',
caption: 'Movie List'
});
$("#bedata").click(function() {
var gr = jQuery("#editgrid").jqGrid('getGridParam', 'selrow');
if (gr != null)
jQuery("#editgrid").jqGrid('editGridRow', gr, { height: 280, reloadAfterSubmit: false });
else
alert("Hey dork, please select a row");
});
});
</script>
<h2>
<%= Html.Encode(ViewData["Message"]) %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
</p>
<table id="editgrid">
</table>
<div id="pager" style="text-align: center;">
</div>
<input type="button" id="bedata" value="Edit Selected" />
这是我的 RegisterRoutes 代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("*MovieService.svc*");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
我的 MovieService 类如下所示:
namespace jQueryMVC
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MovieService
{
// Add [WebGet] attribute to use HTTP GET
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public IList<Movie> GetMovies()
{
return Persistence.GetMovies();
}
}
}
【问题讨论】:
-
你能显示你的路线吗?您可能需要一个 IgnoreRoute 用于 MovieService.svc
-
我已编辑我的回复以显示路线。我为 MovieService 添加了 IgnoreRoute,但它没有改变任何东西,仍然没有调用 Web 服务。
-
您能否在 MovieService.svc 中发布 GetMovies 函数的原型(接口)。您的代码中的问题很清楚。我会重写一点你的代码,让它更容易(并解决你的主要问题)。
-
有趣。刚刚用一个新的测试项目尝试了这个,可以确认行为。似乎不是路由问题,因为您可以直接浏览到 Web 服务。当我有更多时间时,我将不得不玩一下,看看我是否能解决这个问题。
-
我希望我的代码能在你的环境中运行。我对路由没有任何问题,但
routes.IgnoreRoute("*MovieService.svc*")似乎是个好主意。如果某些东西不适用于 MVC/WFC,我们可以比较您和我的项目中的更多部分。再说一句:你怎么能看到我的英文不好,所以如果你发现一些明显的错误,请编辑我的答案并修复这些。
标签: jquery asp.net-mvc ajax jqgrid