Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

prevent POST-request from caching

OP Cris Vilares

2nd May 2014, 11:15 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
1 posts
Join Date:Joined: May 2014
More
Hi,
when I try to make a jquery ajax "Type:Post" request, I get an 404 not found (from cache) error. I've tried several workarounds to fix this problem but it doesn't worked. Here some code snippets to ilustrate my attempts:

This is my Ajax POST request:
Code:
 $.ajaxSetup({
        headers: {
             "X-Requested-With" : "XMLHttpRequest",
             "Content-Type" : "application/atom+xml",
             "cache-control" : "no-cache",
             "DataServiceVersion" : "2.0",
             "X-CSRF-Token" : header_xcsrf_token,
        },          
        cache: false,
    });

    $.ajax({
        type: 'POST',
        url: url + new Date().getTime(),
        data : data,
        beforeSend : function(xhr) {
            // crypto call before authentication
            var bytes = Crypto.charenc.Binary.stringToBytes(user + ":" + password);
            var base64 = Crypto.util.bytesToBase64(bytes);
            //build request header
            xhr.setRequestHeader("Authorization", "Basic " + base64);               
        },
        success: function(data, status, xhr) {
            alert("data sent");
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(textStatus + "    " + errorThrown + "    " + jqXHR.status + "    " + jqXHR.responseText);
        }
    });
I've also tried to avoid the AppCache with following code in JAVA onCreate Method:
HTML Code:
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.init();

    super.appView.getSettings().setAppCacheEnabled(false);
    super.loadUrl(Config.getStartUrl());
    CookieManager.getInstance().setAcceptCookie(true);
    // Set by <content src="index.html" /> in config.xml
    //super.loadUrl("file:///android_asset/www/index.html");
}
every time the Ajax Post request is made, it is directed to cache, and I don't know how to prevent it.
7th May 2014, 10:43 AM   |  #2  
Archer's Avatar
Senior Moderator / Recognized Developer - (Un)Recognized Pole Dancer
Flag Northampton
Thanks Meter: 4,107
 
10
13,899 posts
Join Date:Joined: Jul 2008
More
Quote:
Originally Posted by Cris Vilares

Hi,
when I try to make a jquery ajax "Type:Post" request, I get an 404 not found (from cache) error. I've tried several workarounds to fix this problem but it doesn't worked.


You're probably getting a 404 because you're appending the timestamp onto the url without any seperator. Try this...

Code:
    $.ajax({
        type: 'POST',
        url: url + (url.search("?") == -1 ? "?" : "&") + new Date().getTime(),
        data : data,
        beforeSend : function(xhr) {
            // crypto call before authentication
            var bytes = Crypto.charenc.Binary.stringToBytes(user + ":" + password);
            var base64 = Crypto.util.bytesToBase64(bytes);
            //build request header
            xhr.setRequestHeader("Authorization", "Basic " + base64);               
        },
        success: function(data, status, xhr) {
            alert("data sent");
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(textStatus + "    " + errorThrown + "    " + jqXHR.status + "    " + jqXHR.responseText);
        }
    });
That will append either ?nnnnnnnn or &nnnnnnnn to the url, depending on whether it already has a ? in it.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes