Not sure if my last question was posted…
As I said before, this is not much information on the JavaScript $.ajax call on the DocuSign REST API. If there was, not much information on it (or even examples).
I’m am trying to use JavaScript (JQuery) $.ajax call using a DocuSign REST API. Even though I tested out on Postman successfully, it does not work using $.ajax (i.e. https://account-d.docusign.com/oauth/token) including the header information and the requested body (note the ${???} represent a placement of the actual values - just to be safe):
let tkHeader = {
'Content-Type': 'application/x-www-for-urlencoded',
'Accept': '*/*',
'Accept - Encoding': 'gzip,deflate,br',
'Connection': 'keep-alive',
'Authorization': `Basic ${basicBase64IntSecKey}`
}
let tkRequestBody = {
'grant_type': 'authorization_code',
'code': `${authCode}`
};
(highlighted black on items above represent require files base on Postman forked from DocuSign REST API)
Where ${basicBase64IntSecKey} is the “IntegratorKey” ”Secret Key” with a colon (:) separating them using the following JavaScript Code:
let base64 = btoa(`${integrationKey}:${secretKey}`);
let basicBase64IntSecKey = `Basic ${base64}`;
The code is base on the following URL
The only way to get the Authorization Code is to run this URL on a browser directly and copy authorization code back to the ${authCode}.
Here is a typical JavaScript $.ajax code using https://account-d.docusign.com/oauth/token as an example:
let returnedAuthCode = 'https://localhost/?code=????'; // result from the code URL above
let authCode = returnedAuthCode.substring(returnedAuthCode.indexOf("?code=") + 6); // extracting code
let base64 = btoa(`${integrationKey}:${secretKey}`);
let basicBase64IntSecKey = `Basic ${base64}`;
let tkHeader = {
'Content-Type': 'application/x-www-for-urlencoded',
'Accept': '*/*',
'Accept - Encoding': 'gzip,deflate,br',
'Connection': 'keep-alive',
'Authorization': `Basic ${basicBase64IntSecKey}`
}
let tkRequestBody = {
'grant_type': 'authorization_code',
'code': `${authCode}`
};
var getAccessTokenUrl = 'https://account-d.docusign.com/oauth/token'
And the the $.ajax code:
$.ajax({
type: 'POST',
url: getAccessTokenUrl,
header: tkHeader,
dataType: 'jsonp',
body: JSON.stringify(tkRequestBody),
success: (response) => {
let iDebug = 0; // use this as a break point to see what kind of response recieved back
},
error: function(xhr, status, error) {
let xhrStatus = xhr.status;
let xhrState = xhr.state();
let xhrError = error.charAt(0).toUpperCase() + error.slice(1);
console.error(`POST - ${xhrError} Fetching DocuSign Access Token (${xhrStatus} - ${xhrState})
\n URL: ${getAccessTokenUrl}
\n Header: ${JSON.stringify(tkHeader)}
\n Req Body: ${JSON.stringify(tkRequestBody)}
\n`);
$.equiTrak.onLoadingComplete();
return; // End here...
}
});
The problem is that this $.ajax call get refused (404) or CORS Access-Control-Allow-Origin refuseal and I tried other ways to overcome this. I even include the following configuration in the web.config under the <system.webServicer>:
<httpProtocol allowKeepAlive="true">
<customHeaders>
<add name="X-Frame-Options" value="ALLOW-FROM *" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
</customHeaders>
</httpProtocol>
Sorry about the long explanation but right now I’m at wits end and cannot understand why this is not working or if the $.ajax command cannot be used here.
Best Regards, Terry
Back to Docusign.com


