$(document).ready(function() { ale...">

Simple getJSON doesn't work

JSON (index) function doesn't fire. Any ideas?

<script type="text/javascript">
    $(document).ready(function() {
        alert("This alert is displayed :(");
        $("form[action$='GetQuote']").submit(function() {                
            $.getJSON($(this).attr("action"), $(this).serialize(), function(Result) {
                alert("This alert is not shown :(");
                $("#name").html(Result.name);
                $("#address").html(Result.address);
            });   
            return false;
        });
    });    
</script>

      

CONTROLLERS ...

    public JsonResult GetQuote(string dataName)
    {
        if (dataName != "" || dataName != null)
            return new JsonResult { Data = new Result { name = "Hello", address = "World" } };
        else
            return null;
    }

      

+2


a source to share


2 answers


ASP.NET MVC 2.0 will throw an error if it tries to do this using the default HTTP GET. You can do this by POST, or add an instruction as suggested in this article:

http://mhinze.com/json-hijacking-in-asp-net-mvc-2/



which: return Json (data, JsonRequestBehavior.AllowGet);

+2


a source


First, you should probably use a function $.ajax

and specify "POST" to post your data name.

Second, you probably need to prevent the default event:



$("form[action$='GetQuote']").submit(function(event) {
    if (event.preventDefault) // Older I.E. uses an old DOM model, which dosen't have this event
       event.preventDefault();

    $.ajax(...) // Do your ajax call
    return false; // Once again, for I.E.
});

      

Submit event can override your jQuery related onSubmit

0


a source







All Articles