Click here to Skip to main content
15,889,877 members
Articles / Programming Languages / Javascript
Tip/Trick

Ajax method not calling server side Controller method

Rate me:
Please Sign up or sign in to vote.
2.50/5 (3 votes)
29 Jan 2016CPOL 6.7K   2   2
Solution if Ajax method not calling Controller method every time its invoked

Introduction

This might help in firing the Controller method, everytime its invoked via an Ajax call.

Background

My ajax call was not calling the Controller method on the server side, even though it was invoked everytime.

Using the code

With cache:false, i was able to have the Server side controller method fired everytime, the function was invoked from the page. 

C++
var selectedGuid;

can_Edit_Submit_Approve_Order(selectedGuid) //function call in my page

function can_Edit_Submit_Approve_Order(opportunityId) 
    {
       
        $.ajax({
            url: "/Order/Can_Edit_Submit_Approve_Order/?opportunityId=" + opportunityId,
            type: "GET",
            cache: false,
            success: function (result) {
                
                if (result.length > 0)
                {
                    //custom logic to enable / disable releavant buttons here
                }
                else
                {
                    //code for default button disable
                }
            }
        });
    }

Points of Interest

Without cache:false, i did verify that the controller call was being by-passed/ missed due to some reason (not for the first time though but consistently thereafter).

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I love to code (ASP.NET-C#, SQL, AJAX, HTML, XML, WCF, MVC, JQuery, Kendo, REST Services, Business Analysis are my favourites to work with),play outdoor sports, travel to places, meet new people, be amidst Nature, read inspirational books, listen to music & am a Foodie as well.

Comments and Discussions

 
QuestionAdd a random number to the requested url would help Pin
allentranks9-Feb-16 11:29
allentranks9-Feb-16 11:29 
GeneralReason Pin
Nitij30-Jan-16 18:39
professionalNitij30-Jan-16 18:39 
That is because caching is used to store the information which is supposed to be accessed multiple times in a memory module which provides fast access. jQuery checks the arguments and the request itself to make sure if it is being requested for the first time or not; so it returns the same data in subsequent requests.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.