Skip to content

Instantly share code, notes, and snippets.

@ranjanprj
Created November 30, 2017 11:47
Show Gist options
  • Save ranjanprj/87d823f673e4bb0080de5583d26d2daa to your computer and use it in GitHub Desktop.
Save ranjanprj/87d823f673e4bb0080de5583d26d2daa to your computer and use it in GitHub Desktop.
sap.ui.controller("Main", {
/**
* Called when a controller is instantiated and its View controls (if available) are already created.
* Can be used to modify the View before it is displayed, to bind event handlers and do other one-time initialization.
* @memberOf testcockpit.Main
*/
onInit: function() {
// $.ajaxSetup({
// beforeSend: function(oXhr) {
// oXhr.setRequestHeader("X-CSRF-Token", sTheToken);
// }
// });
//
// $.get("https://test.com:1234/PROJECT_SRV/Jobs('J.17.000004')?$expand=JobToPartner,JobWbs,JobToWbsPartner,MaterialComp,EquipmentAct,PersonnelAct,GeneralActivity&$format=json",
// function(oResult) { console.log(oResult) }
// );
//
this._fetchCSRFToken().then((data, textStatus, request)=>{
this._token = request.getResponseHeader('x-csrf-token');
$.ajaxSetup({
beforeSend: function(oXhr) {
oXhr.setRequestHeader("X-CSRF-Token", request.getResponseHeader('x-csrf-token') );
}
});
}).done(()=>{
this._createData();
});
},
_createData : function(){
console.log(this._token);
var dat = {
"Carrid" : "992",
"Carrname" : "Air India",
"Currcode" : "AI",
"Url" : "http://ai.com"
};
return $.ajax({
url : "proxy/https/test.com:1234/PROJECT_SRV/sap/opu/odata/sap/ZSFLIGHT_GW_SRV/AirlinesSet",
type: "POST", //or POST?
dataType: "json",
contentType: "application/json",
data : JSON.stringify(dat),
headers : { 'x-http-method': 'POST' },
xhrFields:
{
withCredentials: true
},
success: function(){console.log("The call was successful")},
error: function(){console.log("The call was not successful")}
});
},
_fetchCSRFToken : function(){
return $.ajax({
url : "proxy/https/test.com:1234/PROJECT_SRV/sap/opu/odata/sap/ZSFLIGHT_GW_SRV/AirlinesSet",
type: "GET", //or POST?
dataType: "json",
xhrFields:
{
withCredentials: true
},
beforeSend: function (request)
{
request.setRequestHeader("X-CSRF-Token", "FETCH");
},
success: function(data, textStatus, request){console.log("The call was successful")},
error: function(){console.log("The call was not successful")}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment