Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,805,503 Members 36,915 Now Online
XDA Developers Android and Mobile Development Forum

prevent POST-request from caching

Tip us?
 
Cris Vilares
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 1
Join Date: May 2014
Default prevent POST-request from caching

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.
 
Archer
Old
#2  
Archer's Avatar
Senior Moderator / Recognized Developer - (Un)Recognized Pole Dancer
Thanks Meter 4,017
Posts: 13,767
Join Date: Jul 2008
Location: Northampton
Quote:
Originally Posted by Cris Vilares View Post
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.
.
"I live by my own rules, nobody else's, not even my own."
.
My xda links

New User Guide | XDA Forum Rules | 10 Posts - Why and How? | Admins & Mods | My xda Bookmarks

.

.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes