最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send user details (session token) within every AJAX requests (Sencha Touch 2) - Stack Overflow

programmeradmin3浏览0评论

i am building a Sencha Touch 2 Application with userspecific datasets.

Architecture of the App:

Sencha Touch App <=====> Java Server backend with REST Services ( many AJAX requests =) )

What i actually have is:

  • Login the user with username/password

    The app gets initialized and the loginform es into play. After submitting the form as a AJAX request, the server backend checks the userdata and calls the client callback function.

And what i want to do is:

  • The callback function should
    1. create a cookie with the sessiontoken or
    2. store the sessiontoken within the localstorage (!/api/Ext.data.proxy.LocalStorage) or
    3. store the sessiontoken within js variable

Okay, shouldn't be the problem.

But how can i achieve the following:

Most of the data is specific for one user and should be returned by the REST service if needed (by clicking on the navigation,...). How can i send the sessiontoken (see above) within every AJAX request - so the server can provide the suitable datasets (assuming the token is valid)?

Send cookies within AJAX requests

I have already read that cookies gets automaticly added to the request if the url is on the same space, right? The Java Server is on the same domain (localhost:8080) but the cookies aren't available - instead of requests on urls like 'app.json'. I thought that cross-domain-requests are really domain specific?

Send paramaters within AJAX requests

Because the cookies aren't avi i thought about the possiblity of 'manually' adding parameters to the ajax requests. The App will contain many AJAX requests and thats why i dont want to add the token manually - i tried to override the requests function of Ext.Ajax but i failed ;-( :

(function() {
    var originalRequest = Ext.data.Connection.prototype.request;

    Ext.override(Ext.data.Connection, {
        request : function(options) {
            alert("Do sth... like add params");
            return originalRequest.apply(this, options);
        }
    });
})();

ERROR:

Uncaught Error: [ERROR][Ext.data.Connection#request] No URL specified

I also tried to add a listener

Ext.Ajax.add({
    listeners : {
        beforerequest :  function( conn, options, eOpts ){
           alert("Do sth... like add params");
        }
     }
});

ERROR:

Uncaught TypeError: Object [object Object] has no method 'add'

Any idea about how i can add the token?

Or any better way of handling these case?

Thanks!

i am building a Sencha Touch 2 Application with userspecific datasets.

Architecture of the App:

Sencha Touch App <=====> Java Server backend with REST Services ( many AJAX requests =) )

What i actually have is:

  • Login the user with username/password

    The app gets initialized and the loginform es into play. After submitting the form as a AJAX request, the server backend checks the userdata and calls the client callback function.

And what i want to do is:

  • The callback function should
    1. create a cookie with the sessiontoken or
    2. store the sessiontoken within the localstorage (http://docs.sencha./touch/2-0/#!/api/Ext.data.proxy.LocalStorage) or
    3. store the sessiontoken within js variable

Okay, shouldn't be the problem.

But how can i achieve the following:

Most of the data is specific for one user and should be returned by the REST service if needed (by clicking on the navigation,...). How can i send the sessiontoken (see above) within every AJAX request - so the server can provide the suitable datasets (assuming the token is valid)?

Send cookies within AJAX requests

I have already read that cookies gets automaticly added to the request if the url is on the same space, right? The Java Server is on the same domain (localhost:8080) but the cookies aren't available - instead of requests on urls like 'app.json'. I thought that cross-domain-requests are really domain specific?

Send paramaters within AJAX requests

Because the cookies aren't avi i thought about the possiblity of 'manually' adding parameters to the ajax requests. The App will contain many AJAX requests and thats why i dont want to add the token manually - i tried to override the requests function of Ext.Ajax but i failed ;-( :

(function() {
    var originalRequest = Ext.data.Connection.prototype.request;

    Ext.override(Ext.data.Connection, {
        request : function(options) {
            alert("Do sth... like add params");
            return originalRequest.apply(this, options);
        }
    });
})();

ERROR:

Uncaught Error: [ERROR][Ext.data.Connection#request] No URL specified

I also tried to add a listener

Ext.Ajax.add({
    listeners : {
        beforerequest :  function( conn, options, eOpts ){
           alert("Do sth... like add params");
        }
     }
});

ERROR:

Uncaught TypeError: Object [object Object] has no method 'add'

Any idea about how i can add the token?

Or any better way of handling these case?

Thanks!

Share Improve this question edited Aug 17, 2012 at 10:35 Evan Trimboli 30.1k5 gold badges48 silver badges67 bronze badges asked Aug 17, 2012 at 10:00 judomujudomu 5584 silver badges13 bronze badges 2
  • Just a quick advice : Go to read the documentation when you get something like 'has not method xxx' (docs.sencha./touch/2-0/#!/api/Ext.Ajax) If you take a look at the method for Ext.Ajax there's no 'add' method. That's also not the proper way to add a listener. To add a listener, use the 'on' method (docs.sencha./touch/2-0/#!/api/Ext.Ajax-method-on). – Titouan de Bailleul Commented Aug 17, 2012 at 10:49
  • @tdebailleul You're right, my fault. I've added such a interceptor and it seems to work now - i add a new param to every request. – judomu Commented Aug 17, 2012 at 11:09
Add a ment  | 

1 Answer 1

Reset to default 9

Finally i successfully used:

function addAjaxInterceptor(context)
{
   Ext.Ajax.on('beforerequest', function(conn, options, eOptions)
   {
      // add the param to options...
   }, context);
}

Executed from the app (=> addAjaxInterceptor(this)).

But the following solution is more suitable for my situation i think:

Ext.Ajax._defaultHeaders = {
    // params as json
};

(Cause Ext.Ajax is a singleton object and i dont change the params for every request)

发布评论

评论列表(0)

  1. 暂无评论