前提・実現したいこと
XMLHttpRequestでSalesforceに接続したいです。
社内システムとしてIBM Notesを使用しており、
JavaScriptで記述したXpage(Chromeで表示)からSalesforceに接続したいです。
発生している問題・エラーメッセージ
<追記>
CORS (Cross-Origin Resource Sharing)関係のエラーが出ています。
サーバ側の設定方法が不明なのでご教授いただきたく、
ChromeConsole
1Uncaught ReferenceError: onLoadFunc is not defined 2 at view__id1__id91_clientSide_onclick (XAgent_UI.xsp:171) 3 at _3.x_dfe [as _doFireEvent] (@Iq.js:45) 4 at _3.x_fe [as fireEvent] (@Iq.js:45) 5 at HTMLButtonElement.xaep_fpre (@Iq.js:45)
該当のソースコード
JavasScript
1//ファイル名:UI.xsp 2 3// URLを生成 4var url = "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=(省略)&client_secret=(省略)&username=(省略)&password=(省略)"; 5 6// XMLHttpRequestを作成 7try { 8 xmlhttp = new XMLHttpRequest(); 9} catch (e){ 10 // XMLHttpRequestの作成に失敗 11} 12 13// REST APIのセットと送信 14xmlhttp.onreadystatechange = chkResponse; 15xmlhttp.open('POST', url, true); 16xhr.withCredentials = true;//追記 17xhr.addEventListener('load', onLoadFunc, false);//追記 18xmlhttp.setRequestHeader ( "Content-Type","application/x-www-form-urlencoded" ); 19xmlhttp.send(); 20 21// REST APIのレスポンスの処理(正常時のみ処理する) 22function chkResponse() { 23 if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) { 24 25 var ret = xmlhttp.responseText; 26 var obj = JSON.parse( ret ); 27 28 //・・・・(省略) 29 30 } 31}
試したこと
CORSをXAgentなるもので解決できると知り、スニペット流用(参考サイト:CORS問題の解決)で動かしてみたのですが、こちらもSalesforceに接続することができず、何かエラーが起こっているのかどうかの現状の問題を確認する術もなく、XAgentに頼らないより一般的な方法をと思い、質問させていただきました。
Chromeの拡張機能「RestletClient-REST API Testing」を使って、Salesforceの同じURLに対しトークン要求をしてSalesforceのトークンを取得できることは確認できました。
<追記>
Chrome開発者コンソールで確認したところ、CORSによるエラーが出ておりましたので、クライアント側で以下の2行を追加したのですが、サーバ側はどのように設定したらよいのでしょうか。Access-Control-Allow-Originの設定方法がわかりません。。
xhr.withCredentials = true; xhr.addEventListener('load', onLoadFunc, false);
参考サイト
CORS問題の解決
http://guylocke.blogspot.com/2014/12/xagent.html
<追記>
https://qiita.com/tomoyukilabs/items/81698edd5812ff6acb34
Salesforceアクセストークンの要求
https://help.salesforce.com/articleView?id=remoteaccess_oauth_username_password_flow.htm
RestletClient-REST API Testingでの接続
https://web.plus-idea.net/2016/06/salesforce-rest-api-call-setting/
補足情報(ツールのバージョンなど)
クライアントアプリ:IBM Notes Client 9.0.1 FP5/Domino Server 9.0.1 FP8
Xpages表示:GoogleChrome(バージョン70)