4 MVC和jQuery的getJSON [英] MVC 4 and jQuery getJSON

查看:134
本文介绍了4 MVC和jQuery的getJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现一些在大学一个项目的现场搜索。我有以下的code:
MVC操作:

I have to implement some sort of live search in a project at the university. I have the following code: MVC Action:

[Authorize]  
[AcceptVerbs(HttpVerbs.Get)]  
[InitializeSimpleMembership]
public JsonResult Search(string term)  
{  
    var data = ... // get matching item  
    return Json(data, JsonRequestBehavior.AllowGet);  
}  

脚本中查看:

$(document).ready(function() {  
    $("#searchText").keyup(function() {  
        $.getJSON('/Search/Search', { "term": $(this).val() },  function(result) {   
            alert(result);    
            $("#searchText").val(result.d);  
        });  
    });  
});

控制器动作被调用,并返回匹配的对象,但JavaScript函数没有被调用,没有警告框,什么都没有。有什么可以做,以使这项工作?

The controller action is called and returns the matching objects, but the javascript function is never called, no alert box, nothing. What can do to make this work?

推荐答案

有可能是您的数据的JSON序列化的问题。如果你正在尝试直接序列可能包含循环引用您的EF域模型,这可能经常发生,......正确的做法当然是使用视图模型。

There could be a problem with the JSON serialization of your data. This could often happen if you are attempting to directly serialize your EF domain models which might contain circular references, ... The correct approach is of course to use view models.

为了追踪问​​题,请使用 萤火虫 ,并期待在网​​络设置页看到AJAX调用的确切的请求/响应。在那里,你将可以看到由服务器返回的响应将包含错误信息。

In order to track the problem use FireBug and look at the Network tab to see the exact request/response of the AJAX call. There you will be able to see the response returned by the server which will contain the error message.

这篇关于4 MVC和jQuery的getJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆