locked
Reloading scripts after Post RRS feed

  • Question

  • User-895859300 posted

    After i post data to my controller action, i am calling my index page via ajax:

    $("#btnPostData").click(function () {
            $.ajax({
                async: true,
                type: 'POST',
                dataType: 'JSON',
                contentType: 'application/json; charset=utf-8',
                url: '/home/save',
                data: JSON.stringify(listData),
                success: function (data) {
                    alert("Data saved successfully");
                    $.ajax({
                        async: true,
                        type: 'GET',
                        url: '/home/index'
                    });
                },
                error: function (data) {
                    alert('There was a problem');
                }
            });
        });

    That works fine, but for some reason, my included script file doesn't reload.

    I have in my index view :

    <script src="~/Scripts/myScripts.js"></script>

    In my script file i am initiating a array

    $(document).ready(function () {
        
        var listData = new Array();
        //do some things here
        });

    But after the post and calling the index view, my array remains the same, it doesn't refresh, because the script file doesn't reload. Only after i press the refresh button on the browser, it reloads again.

    How to solve this?

    Thanks in advance.

    Monday, July 27, 2020 9:40 AM

Answers

  • User1686398519 posted

    Hi kaardib,

    Do you need to refresh the Index page after successfully requesting an action named save with ajax?Based on the code you provided, I'm not sure if you are requesting an action named "save" on the "Index" page or on other pages.The following are the solutions I gave, you can modify according to your needs.

    1. If all the codes you give are on the same page, and you just want to refresh the current page after the save request is successful, you only need to modify your code like this.
      • success: function (data) {
             alert("Data saved successfully");
             window.location.reload();
        }
    2. If you just need to jump to the Index page after requesting an action named save, you can modify your code like this.
      • success: function (data) {
             alert("Data saved successfully");
             window.location.href = "@Url.Action("Index")";
        }
    3. In addition, if you need to jump after ajax requests an action named Index, you only need to modify your code like this.
      • success: function (data) {
             alert("Data saved successfully");
             $.ajax({
                   async: true,
                   type: 'GET',
                   url: '@Url.Action("Index")',
                   success: function(data) {
                          //Write code according to your needs
                          window.location.href = "@Url.Action("Index")";
                   }
             });
        }

    Best regards,

    Yihui Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2020 5:38 AM

All replies

  • User475983607 posted

    But after the post and calling the index view, my array remains the same, it doesn't refresh, because the script file doesn't reload. Only after i press the refresh button on the browser, it reloads again.

    How to solve this?

    The response is returned to the AJAX callback function.   The browser does NOT refresh the page.  It is up to you to write code that updates the page.  You'll need to rethink the design to use AJAX. 

    Monday, July 27, 2020 10:37 AM
  • User-895859300 posted

    You'll need to rethink the design to use AJAX

    What do you mean by that? how would you do it?

    Monday, July 27, 2020 1:03 PM
  • User475983607 posted

    What do you mean by that? how would you do it?

    The key is understanding how AJAX works.  Once you understand the fundamentals then you should be able to craft a solution.  

    A quick Google search can also help as this AJAX is a very common feature and there's tons of online examples.

    Monday, July 27, 2020 8:41 PM
  • User-474980206 posted

    you will need to extract the code out of $.ready() to be called from ajax call (or use an event). before calling, you will need to update any data this code used. also you may need to limit where the code runs if it updates the dom (may already have). also you will need to rebind even to dom objects you destroyed, but don't double bind.

    we have no idea what your code is trying to do, so its had to give real help. why are you using ajax if you want the page to refresh?

    Monday, July 27, 2020 9:09 PM
  • User1686398519 posted

    Hi kaardib,

    Do you need to refresh the Index page after successfully requesting an action named save with ajax?Based on the code you provided, I'm not sure if you are requesting an action named "save" on the "Index" page or on other pages.The following are the solutions I gave, you can modify according to your needs.

    1. If all the codes you give are on the same page, and you just want to refresh the current page after the save request is successful, you only need to modify your code like this.
      • success: function (data) {
             alert("Data saved successfully");
             window.location.reload();
        }
    2. If you just need to jump to the Index page after requesting an action named save, you can modify your code like this.
      • success: function (data) {
             alert("Data saved successfully");
             window.location.href = "@Url.Action("Index")";
        }
    3. In addition, if you need to jump after ajax requests an action named Index, you only need to modify your code like this.
      • success: function (data) {
             alert("Data saved successfully");
             $.ajax({
                   async: true,
                   type: 'GET',
                   url: '@Url.Action("Index")',
                   success: function(data) {
                          //Write code according to your needs
                          window.location.href = "@Url.Action("Index")";
                   }
             });
        }

    Best regards,

    Yihui Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2020 5:38 AM