function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VarunCVarunC 

Strange error in JS Remoting : "Remoting request invalid for your session. Refresh page and re-submit request"

I am receiving this error message as response to my JS Remoting Call:
action: "Unknown"
data: ""
message: "Remoting request invalid for your session.  Refresh page and re-submit request"
method: "Unknown"
ref: false
statusCode: 402
type: "exception"
vfDbg: true
vfTx: true
where: ""

I couldn't understand why would this error come, can anyone suggest how to resolve this error? Strangest thing is, it comes in most of the time ai load my VF tab first time, if I press 2-3 times F5 keey and then the JS remoting call works fine.



AshwaniAshwani
Javascript remoting as a builtin functionlity to save itself from CSRF.  Data going over internet is checked for validation at Salesforce server side. If it finds any iisue in CSRF it throw following error:

{
    "type": "exception",
    "tid": 7,
    "ref": false,
    "action": "VisualHandler",
    "method": "testRemote",
    "message": "Remoting request invalid for your session.  Refresh page and re-submit request",
    "where": "",
    "data": [
      "a0pd0000002gHjlAAE"
    ],
    "vfTx": true,
    "vfDbg": true
  }

If you are facing this iisue you can disable the CSRF protection from Setup menu. However it is disabled by default.
VarunCVarunC
But I wouldn't want my users of the app to edit there security settings. And I've not touched them myself in my dev org as well.
Is it normal behavior for this to get corrupt?

Below are the details of my code:
<script type="text/javascript">
    $j = jQuery.noConflict();
    var mtid = '{!JSINHTMLENCODE(mi)}';
    $j(document).ready(function(){
        advpm.RemotingController.getListItems(new Date( $j('.s-date').text() ).toUTCString(), new Date( $j('.e-date').text() ).toUTCString(), mtid, $j('.opt1').is(':checked'), $j('.opt2').is(':checked'), $j('.opt3').is(':checked'), $j('.opt4').is(':checked'), $j('.opt5').is(':checked'), function(result, event){
            if (event.status && result){
                //do something with result here...
            } else {
                //error added to page here...
            }
        }, {escape:true});
    });
</script>

APEX Method code:
@ReadOnly @RemoteAction 
global static list<wListItems> getListItems(Date da0, Date da1, string matId, boolean showEvents, boolean showMyActivitiesOnly, boolean showMyMTOnly, boolean showActivitiesWithMTOnly, boolean showRelatedMT)
{
    list<wListItems> allitems = new list<wListItems>();
    Datetime frmDt = Datetime.newInstance(da0.year(), da0.month(), da0.day());
    Datetime toDt = Datetime.newInstance(da1.year(), da1.month(), da1.day());

    if (showEvents) 
    {
        list<Event> eventsList      = new list<Event>();
        string qry                  = 'select Id, Subject, WhatId, What.Name, ActivityDate, StartDateTime, EndDateTime, IsAllDayEvent, Owner.FirstName, Owner.LastName from Event where ActivityDate >= '+frmDt.format('yyyy-MM-dd')+' and ActivityDate <= '+toDt.format('yyyy-MM-dd')+(showMyActivitiesOnly == true ? ' and OwnerId = \''+ Userinfo.getUserId() +'\'' : '')+(matId != null && matId != '' ? ' and WhatID =: matId' : (showActivitiesWithMTOnly == true ? ' and WhatID IN (select Id from Case) and What.Type = \'Case\'' : '')+' order by StartDateTime');
        for(Event e : (list<Event>)database.query(qry))
        {
            allitems.add( new wListItems(e.Id,e.Subject,'',e.ActivityDate,'Event','') );
        }
    }
    return allitems;
}

This call when viewed in Chrome Console (the request headers):
action: "advpm.RemotingController"
ctx: {,…}
data: [Sun, 27 Jul 2014 00:00:00 GMT, Sun, 3 Aug 2014 00:00:00 GMT, , true, false, false, false, true]
method: "getListItems"
tid: 5
type: "rpc"
The parameters view in Chrome Console:
0: "Sun, 27 Jul 2014 00:00:00 GMT"
1: "Sun, 3 Aug 2014 00:00:00 GMT"
2: ""
3: true
4: false
5: false
6: false
7: true
Chrome Console "Preview" tab - http://screencast.com/t/wJMB4ixwx
action: "Unknown"
data: ""
message: "Remoting request invalid for your session.  Refresh page and re-submit request"
method: "Unknown"
ref: false
statusCode: 402
type: "exception"
vfDbg: true
vfTx: true
where: ""
and the dreaded Console Exception logged - http://screencast.com/t/pDFZQqJ6F9
Uncaught TypeError: Cannot read property 'tid' of undefined VFRemote.js:85
VFExt3.Direct.VFExt3.extend.getTransaction VFRemote.js:85
VFExt3.Direct.RemotingEvent.VFExt3.extend.getTransaction VFRemote.js:88
(anonymous function) VFRemote.js:131
a.Event.fire VFRemote.js:52
a.Observable.fireEvent VFRemote.js:47
VFExt3.direct.RemotingProvider.VFExt3.extend.onData VFRemote.js:94
VFExt3.extend.handleResponse VFRemote.js:75
a VFRemote.js:39
(anonymous function) VFRemote.js:40


The thing is, console is overwriting the actual Error message on why my JS remoting call is getting failed, and when I digg deaper, I couldn't understand why would the CSRF token get corrupted. I'm only initiating JS Remoting call when document is ready.