I am trying to execute the CRUD operation in SAPUI5 using OData.request.
I could not execute the below codes as it says Unauthorized 401 even though I have put the correct user and password.
If I execute the URL in the Chrome browser, I can get the result. Any clue ?
Here is the code.
onEditMaterial: function() { var matid_var = sap.ui.getCore().getControl("matid").getValue(); var matldesc_var = sap.ui.getCore().getControl("matldesc").getValue(); jQuery.sap.require("sap.ui.model.odata.datajs"); OData.request ({ requestUri: "http://ASPSGPLR81BC0G/sapgw/sap/opu/odata/sap/ZGW_MATERIAL_SRV/Materials('" + matid_var +"')", method: "GET", user : 'username', password : 'password', headers: { "X-Requested-With": "XMLHttpRequest", "Content-Type": "application/atom+xml", "DataServiceVersion": "2.0", "X-CSRF-Token":"Fetch", }, }, function (data, response) { header_xcsrf_token = response.headers['x-csrf-token']; OData.request ({ requestUri: "http://ASPSGPLR81BC0G/sapgw/sap/opu/odata/sap/ZGW_MATERIAL_SRV/Materials('" + matid_var + "')", method: "POST", user : 'username', password : 'password', headers: { "X-Requested-With": "XMLHttpRequest", "Content-Type": "application/atom+xml", "DataServiceVersion": "2.0", "Accept": "application/atom+xml,application/atomsvc+xml,application/xml", "X-CSRF-Token": header_xcsrf_token }, data: { Material: matid_var, MatlDesc: matldesc_var } }, function (data, response) { console.log(data); }, function (err) { console.log(err.response); } ); }, function (err) { var request = err.request; // the request that was sent. var response = err.response; // the response that was received. alert("Error in Get -- Request "+request+" Response "+response); } ); },