Asp.net mvc 2 jqueryajax无法正常工作?

Asp.net mvc 2 jqueryajax无法正常工作?,asp.net-mvc-2,Asp.net Mvc 2,我对这个jquery脚本有部分看法: $("#btnGetEmpInfo").click(function () { var selectedItem = $('#EmployeeId').val(); var focusItem = $('#EmployeeId') alert("Starting"); $.ajax({ type: "GET", contentType: "

我对这个jquery脚本有部分看法:

$("#btnGetEmpInfo").click(function () {
          var selectedItem = $('#EmployeeId').val();
          var focusItem = $('#EmployeeId')
      alert("Starting");
          $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "<%= Url.Action("getEmpInfo", "NewEmployee")%>?sData=" + selectedItem,
            data: "{}",
            success: function(data) {
                if (data.length > 0) {
                alert("Yeah!");
                } else {
                   alert("No data returned!");
                }
              }                   
            });
            alert("Back!");
      });  
我可以在控制器中设置断点,它正在命中,但我得到的唯一“警报”是“开始”和“返回”。为什么不返回数据,或者至少说没有返回数据

提前感谢您的帮助


Geo…

您可能希望改进此ajax调用,如下所示:

$.ajax({
    type: 'GET',
    url: '<%= Url.Action("getEmpInfo", "NewEmployee")%>',
    data: { sData: selectedItem },
    success: function(data) {
        // Warning: your controller action doesn't return an array
        // so don't expect a .length property here. See below
        alert(data.Data);
    }                   
});

好的,现在我们已经修复了错误,让我详细说明一下。在代码中,您使用了
应用程序/json
内容类型来格式化请求字符串。不幸的是,在ASP.NETMVC2中,没有任何现成的东西能够理解JSON请求(除非您编写了一个脚本)。然后使用字符串连接将
sData
参数附加到URL,而不进行URL编码,这意味着当用户在
EmployeeId
文本框中输入一些特殊字符(如
&
)时,您的代码将中断

尝试添加“beforeSend”、“error”和“complete”回调以在javascript调试器中获取更多信息


您正在使用javascript调试器吗?(firebug、ie9开发工具、chrome开发工具都是不错的工具)

虽然它更漂亮,但仍然没有达到上面提到的警报。@George,怎么样?你试过了吗?您看到作为请求发送的内容和服务器的响应了吗?您是否删除了
?sData=“+从请求的url部分选择EdItem
?您是否从请求中删除了
应用程序/json
内容类型?您是否在服务器控制器操作中添加了
JsonRequestBehavior.AllowGet
?您是否删除了成功回调中的
数据.length
?我的道歉…没有通读一遍。添加了控制器的部件,现在返回“无数据”,即使它正在通过测试。嗯…@George,您是否删除了
成功
回调中的
data.length
测试?您的服务器不发送数组,因此不要期望使用长度属性。这就成功了。再次感谢你的帮助。我真的很感激。很高兴认识能够帮助我们MVC新手的用户!;-)
$.ajax({
    type: 'GET',
    url: '<%= Url.Action("getEmpInfo", "NewEmployee")%>',
    data: { sData: selectedItem },
    success: function(data) {
        // Warning: your controller action doesn't return an array
        // so don't expect a .length property here. See below
        alert(data.Data);
    }                   
});
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getEmpInfo(string sData)
{
    return Json(new { Data = "test" }, JsonRequestBehavior.AllowGet);
}