1

i have a problem with ajax post function. Ajax function doesn't working without alert message. If add alert message, div content will be refresh and working my code.

I have a mv3 application. And post form data to this javascript function. Like that:

    function Add_Definition() {
        var data = $.extend({ call: "Add_Definition", url: "/Definition/Definition", type: "post", dataType: "html", data: $("#definition_form").serialize() }, data);
        Load(ajaxrequest(data));
    }


    function Load(val) {
        if (val == true) {
            var data = $.extend({ call: "Load", render: $("#render"), url: '@Url.Action("Definition", "Definition")', type: "get", dataType: "html", data: { "groupid": '@Request.QueryString["groupid"].ToString()'} }, data);
            ajaxrequest(data);
        }
    }

My ajax javascript function is below.

function ajaxrequest(param) { var result = true;

//alert("hi there");

$.ajax({
        type: param.type,
        url: param.url,
        data: param.data,
        dataType: param.dataType,
        success: function (result) {
             $(param.render).empty();
                $(param.render).html(result);
            result = true;
        },
        error: function (request, status, error) {
           result= false;
        }
    });
return result; }

If remove // char from javascript code, it will not work, (need refresh page), if have a alert message, content will be refresh with load function.

How is error?

Caner
  • 813
  • 1
  • 12
  • 26
  • 1
    Because ajax is asynchronous, and your alert is giving it just enough time to get the data before proceeding. Try making it synchronous. http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax – ndugger May 22 '14 at 15:16
  • To work you need to add async:false , and everything will be ok. – Nic May 22 '14 at 15:33
  • Is this the same issue with my problem (from a few weeks ago). Just found this article, my post is very similar https://stackoverflow.com/questions/52472455/what-would-cause-my-ajax-to-not-follow-through-all-commands/52473225?noredirect=1#comment91960932_52473225 –  Oct 04 '18 at 23:41

1 Answers1

0

you can also direct call like

var divElem = $('#yourdivid').load('@Url.Action("Definition", "Definition")');
banny
  • 859
  • 7
  • 12