5

In my AngularJS app I am sending HTTP GET request as below.

MyService.HttpReq("testUrl", "GET", null);

HttpReq Method is defined in a service and implemented as below:

this.HttpReq = function(URL, method, payload)
{
    $http({
        url: URL,
        method: method,
        cache: false,
        data: postData,
        headers: {
            'Content-Type': 'application/json',
        }
    }).success(function(response)
    {
        console.log("Success: "+JSON.stringify(response));

    }).error(function(data, status)
    {
       console.error("Error");

    });
}

First of all is this the right way of sending HTTP request in AngularJS?
The problem that I am facing is, some times I get cached data as response and HTTP request is not hitting the server. what can be the issue?

UPDATE

As per the comment and answer I have updated my HTTP request code as below, but still getting same issue.

this.HttpReq = function(URL, method, payload)
{
    $http({
        url: URL, 
        method: method, 
        cache: false,
        data: payload,
        headers: {
            'Content-Type': 'application/json',
            'Cache-Control' : 'no-cache'
        }
    }).
    then(
        function(response) 
        {
            var data = response.data;
            console.log("Success: "+JSON.stringify(data));
        }, 
        function(response) 
        {
            var data = response.data || "Request failed";
            var status = response.status;
            console.error("Error: "+JSON.stringify(data));
        }
    );
}
9
  • What method do you use in your HttpReq method? GET or POST..or both? Commented Jun 15, 2016 at 13:26
  • I use Both GET and POST Commented Jun 15, 2016 at 13:29
  • .success and .error are deprecated, use 2 callback functions instead. Commented Jun 15, 2016 at 13:33
  • Can you show how to use it? Commented Jun 15, 2016 at 13:35
  • 1
    Technically it is feasible. Using the keyworkds "angular promise chaining" should get you some results. In general we try to make as few API calls as possible. In a "real" app you would be required to change your API so that it returns all the data you need in 1 request. Commented Jun 15, 2016 at 14:24

2 Answers 2

2

IE Browsers will catch ajax get requests even if we add cache control headers to the response. Only way i found to solve the issue is to add some random parameter to the request. Please make sure the api have no problem even if you send extra parameters

 MyService.HttpReq("testUrl?ts=" + Date.now(), "GET", null);
Sign up to request clarification or add additional context in comments.

2 Comments

OH! I am using chrome for testing but still I am facing this issue.
There is no use of adding cache-control header to the request. It should be set to response header in server side. Please try adding a random parameter like timestamp as i shown in example. It should work.
1

Just add cache: false attribute to config object.

https://docs.angularjs.org/api/ng/service/$http#caching

Also you can add header: 'Cache-Control' : 'no-cache'

6 Comments

Please check my updated question, I have just now tried with cache: false but I still see cached data.
$http doesn't cache data by default.
Calls that I am doing are in sequence, in success call back of one I am sending another request, is that the right way?
Also you can add header: 'Cache-Control' : 'no-cache'
@A_user The example at the bottom of the doc for $http is the right way to have a generic function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.