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
WEN JIEWEN JIE 

Ues jquery.ajax to invoke apex class

Hi,

 

I want to use jquery to invoke the Apex class. These jquery are written in a HTML page not visualforce page.

 

I have written some JavaScript codes to get the access_token base on OAuth2.0, so when user allow my application access their data, I want to invoke my Apex class in Force.com.

 

The following is my apex class which used the REST architecture.

 

@RestResource(urlMapping='/myservice/*')
global with sharing class MyRestResource{
 
    @HttpGet
    global static list<sObject> getRecord(RestRequest req, RestResponse res)
    {
        String objectType = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        String queryString = 'select name, id from ' + objectType;
        
        List<sObject> objects = Database.query(queryString);
        return objects;
    }
}

 

It will listen the request url which contain /myservice/*

 

Then I use cUrl tools to test this class. By the following command the apex can intercept my request url and return the right result.

 

curl -X GET https: //ap1.salesforce.com/services/apexrest/myservice/Account/ -H "Authorization: OAuth access_token" -H "X-PrettyPrint:1"


So I think I can use this url (https://ap1.salesforce.com/services/apexrest) and packaged the other parts in a jquery.ajax url to achieve the same thing.

But when I did it in a jquery.ajax method I found the ajax can't sent the request by this url pattern.

Code section:

 

$.ajax({
    type:"GET",
    url:"https://ap1.salesforce.com/services/apexrest/myservice/Account",   
    error: function(){alert("error");},
    success: function(){
            alert("Hello");
    },
    beforeSend: function(jqXHR) {
          jqXHR.setRequestHeader("Authorization", "OAuth " + oauthResponse.access_token);
    }
});

But this request can't sent successful by ajax method.

So I think the request url which sent by curl maybe analysis to another pattern in Force.com.

  

Does anyone have experiences in this field which use jquery invoke apex in a HTML page?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
WEN JIEWEN JIE

I found we can't invoke apex cross domain.

I have saw some information from this bolg, I think it will help others.

 

http://iwritecrappycode.wordpress.com/  by Kenji

 

Thanks.

All Answers

WEN JIEWEN JIE

I found we can't invoke apex cross domain.

I have saw some information from this bolg, I think it will help others.

 

http://iwritecrappycode.wordpress.com/  by Kenji

 

Thanks.

This was selected as the best answer
Rakesh KumarrRakesh Kumarr

Hi ,

 

can anyone provide an solution for the above problem.

 

I want to call an appex web service from jquery code using ajax with session id.

 

please reply soon.

 

thanks 

WEN JIEWEN JIE

Hi,

 

You can get some useful info from this blog http://iwritecrappycode.wordpress.com/