locked
how can I call Get method from java-script ajax and store the result into global variable RRS feed

  • Question

  • User-1355965324 posted

    I am trying to call  a GET method in controller from javascript and trying to store  the result into a global variable resultval in javascript . Here is my code.  But when I run the program , the variable resultval always showing as undefined. But the  controller method is being called and  the result is stored in   return Json(mappingList)

    var resultval
     $.ajax({
            type: "GET",
            url: "/User/GetDepotDepartemntsForMap",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
              resultval = data;
    
                });
            },
            failure: function (response) {
                console.log(response.responseText);
            },
            error: function (response) {
                console.log(response.responseText);
            }
        });
        }
    consol.log(resultval); here it is coming as undefined
     public JsonResult GetDepotDepartemntsForMap()
            {
          
                dynamic mappingList = new List<DepotMapModel>();
                mappingList = _unitOfWork.Department.GetDepotWithDepartment();         
                return Json(mappingList);
    
            }



     

    Thursday, April 22, 2021 10:09 PM

Answers

  • User1686398519 posted

    Hi polachan, 

    By default, ajax requests are asynchronous, which means that the browser will not wait for them to complete before continuing to work.

    • You can check the order of execution below.

    This is the solution to set the ajax request to be synchronous.

    • But asynchronous programming makes the user experience better.
    • You can choose to process the returned data directly in the success function of ajax without assigning it to a global variable.
        console.log("1.start");
        var resultval = "test";
        console.log("2.start resultval:" + resultval);
        console.log("3.ajax: start")
        $.ajax({
            url: "@Url.Action("getdata")",
            type: "GET",
            data: { search: "" },
            async: false,
            success: function (data) {
                console.log("4.ajax:success start")
                resultval = data;
                console.log("5.ajax:success end")
            }
        });
        console.log("6.ajax: end")
        console.log("7.end resultval:" + resultval);
        console.log("8.end");

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 23, 2021 6:43 AM

All replies

  • User-1545767719 posted

    I suggest that you use the Fiddler (or another tool) to capture the reaquest and response between client and server and to see if there are contents as expected.

    Thursday, April 22, 2021 10:47 PM
  • User1686398519 posted

    Hi polachan, 

    By default, ajax requests are asynchronous, which means that the browser will not wait for them to complete before continuing to work.

    • You can check the order of execution below.

    This is the solution to set the ajax request to be synchronous.

    • But asynchronous programming makes the user experience better.
    • You can choose to process the returned data directly in the success function of ajax without assigning it to a global variable.
        console.log("1.start");
        var resultval = "test";
        console.log("2.start resultval:" + resultval);
        console.log("3.ajax: start")
        $.ajax({
            url: "@Url.Action("getdata")",
            type: "GET",
            data: { search: "" },
            async: false,
            success: function (data) {
                console.log("4.ajax:success start")
                resultval = data;
                console.log("5.ajax:success end")
            }
        });
        console.log("6.ajax: end")
        console.log("7.end resultval:" + resultval);
        console.log("8.end");

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 23, 2021 6:43 AM
  • User-1355965324 posted

    Many Thanks

    Friday, April 23, 2021 9:16 AM