0

I have an ASP.net MVC project and depending on the filter options chosen by the user I am sending different ajax requests to the same actionresult, for example:

 $(document).on("click", "#filter_reset_button", function () {
    var url = "/Admin/Index";

    ajaxRequest({
        url: url,
        type: "get",
        data: { reset: true },
        successCallback: function () {
            window.location.href = url;
        }
    });
});

Other listeners sent different data, something like:

 data: { page: 2, filterUpdate: true }

and so on. The Index ActionResult returns different lists of items, depending on different options chosen in the data and the code works completely fine.

A colleage of mine told me, that my code is actually sending two get requests to the AR everytime, so its not efficient. Is that true? And if its the case, how can I refactor it. to make it just one request? If I let window.location.href = url part out, the site actually doesnt load the server response.

MichaelK
  • 31
  • 4

3 Answers3

0

That is correct 2 request called.

First request when you call AJAX get to Action Index in Admin Controller.

Second request when you set window.location.href = url, it will same as you enter /Admin/Index in browser.

In this case you only need window.location.href = '/admin/index?reset=true' in click function

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • So is there a way to improve my code? Because without the window.location.href my browser doesn`t load the server response into the window. – MichaelK Apr 05 '19 at 14:25
  • you dont need ajax in this case, you can only use /admin/index?reset=true – Hien Nguyen Apr 05 '19 at 14:27
  • It sadly wouldn`t work, we have some complex logic in the Index AR and reset is one of many parameters, so basically if I do /admin/index?reset=true it deletes another options which were already saved, but if I do Ajax request and windows.location.href after that, reset gets saved into the AR and the new page loads with all the previously saved options (reset = true included). – MichaelK Apr 05 '19 at 14:42
  • You need return data in result ajax and update UI base on json data – Hien Nguyen Apr 05 '19 at 14:43
  • Could you explain it a little bit more in depth? I am not sure I understand how to do what you have written there. – MichaelK Apr 05 '19 at 14:54
  • In index action, you need check Request.IsAjaxRequest, if yes you need return Json( Data = { your data } ), in success function of ajax, you iterate data and display in UI by jquery – Hien Nguyen Apr 05 '19 at 14:56
0

Yes you are doing 2 request in button click. First in Ajax Get, Second in Success Call Back.

But Why are you calling window.location.href = url; success call back. ?

If you want update the page after click, you can do partial updates to page. Check this post.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
0

You can see the post here at this post

Actually on success callback you must change your code accordingly to the above post

SpyrosLina
  • 131
  • 7