I'm trying to call into Salesforce via Ajax from another site. I've verified the content of my request is correct using a service to call in using the same JSON and auth token and everything, but when I try it in Javascript it gives me an "INVALID_SESSION_ID" error. Here's the Javascript:

var $j = jQuery.noConflict();
    type: "POST",
    //caps below are names changed to protect the innocent
    url: "https://INSTANCE.salesforce.com/services/apexrest/RESOURCE", 
    headers: {
        'Authorization': "OAuth MY_TOKEN_HERE", //token is valid.
        'Content-Type': 'application/json',
    crossDomain: true,
    data: newLeadInfoJson, //this is some JSONified data
    dataType: 'jsonp',
    success: function(responseData, status, xhr) {
    error: function(request, status, error) {

As for the resource itself, as I said when I use an online service to do the call into salesforce it goes through splendidly and works like a charm.

My question revolves around this INVALID_SESSION_ID. Clearly it's not invalid, it's the one I'm using right now, and it's the same one I used in that call in from the online service, which worked! What am I missing?

I have verified the session settings allow for other IPs to use the same sessions. I have also checked that my user is API enabled. I know my instance is correct, and I know my resource url is also correct.

EDIT: minor breakthrough, I got it working in visualforce by changing "jsonp" to "json" in the datatype attribute. I still cannot get it to work from my local machine, though. It will do the request but the result won't show up in Salesforce.


I am not sure whether what you are doing is completely secure from CORS perspective, but there is XMLHttpRequest.withCredentials option that you can use in your jQuery:

url: 'http://bar.other',
data: { whatever:'cool' },
type: 'GET',
beforeSend: function(xhr){
   xhr.withCredentials = true;

Basically, by default your session cookies will be ignored by default.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.