开发者

Simple getJSON does not work

开发者 https://www.devze.com 2022-12-29 05:56 出处:网络
JSON function(Index) does not fire.Any Ideas? <script type=\"text/javascript\"> $(document).ready(function() {

JSON function(Index) does not 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...

开发者_如何学Python    public JsonResult GetQuote(string dataName)
    {
        if (dataName != "" || dataName != null)
            return new JsonResult { Data = new Result { name = "Hello", address = "World" } };
        else
            return null;
    }


ASP.NET MVC 2.0 will throw an error if trying to do this with a HTTP GET by default. You can either make it a POST or add the instruction as suggested in this article:

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

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


Firstly, you probably should use the $.ajax function, and specify a "POST" to post your dataName.

Secondly you probably need to prevent the default event from happening:

$("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.
});

The submit event may be overriding your onSubmit bound with jQuery

0

精彩评论

暂无评论...
验证码 换一张
取 消