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
kdaviskdavis 

How to retrieve CSRF token from form

Hi Developers,

I am attempting to make a request to the endpoint /oppitm/lineitemsort.jsp from a visual force page to sort OLIs. However, it is my understanding that in order to make post request like this I will need the CSRF token present in the request. I am trying to do this but I am having difficutly getting the correct token. I try to retrieve the token with the following ajax request:
$j.ajax({
                 url:"./oppitm/lineitemsort.jsp?id={!oppId}",
                 contentType:"text/html",
                 xhrFields:{withCredentials:true},
            }).done (function(data){
                key_start='csrfToken="';
                key_end='"';
                pos_start=data.indexOf(key_start);
                pos_end=data.indexOf(key_end,pos_start+key_start.length);
                var token=data.substring(pos_start+key_start.length,pos_end);
            });
});

But I continually get redirected to "Unable to access page." when using the CSRF token that is returned.  My understanding is that Visual Force will store the CSRF token in the form under an input with id "com.salesforce.visualforce.ViewStateCSRF" following a post, so I have been trying to get the token that way. But all attempts to do so result in an empty value in that element. Any ideas when that is appending to the form and how I can get it?
Mitesh SuraMitesh Sura
You may want to follow this thread: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008zkRIAQ
SarikaPSarikaP
@Kdavis, Were you able to get the solution? 
@Mitesth, i tried to follow the thread mentioned above, but its stil not working for me.