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
Pasquale IodicePasquale Iodice 

Einstein vision api from javascript

Hi everyone,
I'd like to create a my own custom model. 
I did it using cURL without issues. Now I'm trying to get the same results using Javascript to have a token, create e datamodel, train a dataset ad have a prediction programmatically.
I'm having issues gettin a token from an ajax request.
This is my code to generate an assertion a get a token: 

function generate_assertion(sub, exp, privKey) {
            var header = JSON.stringify({
              "alg": "RS256",
              "typ": "JWT"

            var timeInMillis = Math.round(+new Date() / 1000);
            exp = timeInMillis + (parseInt(exp) * 60);
            var domain = ""

            var payload = {
              "sub": sub,
              "aud": domain + "/v1/oauth2/token",
              "exp": exp
            assertion = KJUR.jws.JWS.sign(null, header, payload, privKey);
            var tokenPayload = {
              "assertion": assertion,
              "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer"
            return tokenPayload;
        // On form submit generate the access token
        function getToken() {
            var sub = '';
            var exp = 7200;
            var tokenPayload = "";

            try {
            tokenPayload = generate_assertion(sub, exp, privKey);
            } catch (err) {
                console.log("ERROR: ", err);

            type: 'post',
            url: '',
            headers: 'Content-type : application/x-www-form-urlencoded',
            data: tokenPayload,
            success: function (data) {
                console.log("TOKEN: " , data["access_token"]);
            error: function (data) {
                  console.log("ERROR: " , data);

Where "privKey" is the private key contained in the "predictive_services.pem" file I downloaded when I signed up for an account.

 "generate_assertion" Methos seems work fine. I'm having this ERROR in ajax request:

"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Best regards,

Nayana KNayana K

content type is not form -urlencoded. It should be mulriform data. Please go through above project in trailhead. It has code to crreate/train/delete  data set and for prediction in Einstein vision.  This is lightning component.  Go through apex classes  provided and try to understand the underlying logic.  It may help you to achieve the same thing in ajax
Pasquale IodicePasquale Iodice
Hi Nayana,
thanks for your response!!
After trying different solutions, I found that the problem depends on a server-side setting.
I think that "Access-Control-Allow-Origin","<external domains>" setting is missing.
In this way you can't access the web service "Einstein Vision (Metamind)" from javascript. I tried it from cURL and from Apex and it works fine.
It might be a good idea to enable on server  the access from domains  to allow developers to use javascript to call APIs.
Best regards,