1

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);  
}  

Script in View:

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

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?

1
  • 2
    Check the console for errors as there is nothing specifically wrong with the code you've posted. Commented Jan 27, 2013 at 14:23

1 Answer 1

3

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, the serialization was the problem. Using FireBug was the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.