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
PinkeyPinkey 

Session expired or invalid on ajax POST

anyone know why below code errors out with a message "Session expired or invalid"

.ajax({
                    url: '/services/data/v28.0/sobjects/Attachment/',
                    type: 'POST',
                    data: '{"ContentType": "image/tiff", "Name" : "REST_API_Application", "ParentId": "'+self.model.get('Id')+'", "Body": "'+responseData.data+'"}',
                    contentType: 'application/json',
                    processData: false,
                    beforeSend: function(xhr) {            
                        xhr.setRequestHeader('Authorization', 'Bearer '+sessionId);           
                    }


session id is defined in VF page
var sessionId = '{!$Api.Session_ID}';

 
Daniel BallingerDaniel Ballinger
The problem is that not all Session IDs are created equally. See Different Session Ids in Different contexts (http://salesforce.stackexchange.com/questions/23277/different-session-ids-in-different-contexts)

A SessionId requested from a Visualforce page on the domain c.podInstance.visual.force.com won't be a "first class" citizen in the way that on from podinstance.salesforce.com or an OAuth flow would be. There are restrictions on what API calls it can be used for.