on ‎2018 Jul 24 7:12 AM
Hi all,
I have an application in my WEB IDE where I use both XSODATA and XSJS service in my application.I am trying to perform posting using XSJS service in my application. I tried ajax call that i referred in various blogs. But I get response as 405 Bad Method.
Here's the code what I have tried.
Method-1:
var postPayLoad ={
CUSTOMERID: "123603596",
CUSTOMERNAME: "Mohammed Ahmed",
CUSTOMEREMAIL: "testmail@qgtc.com",
SERVICENAME: r_seaman_cell0,
VESSELNAME: "LNG Vessel",
PORTNAME: "Alruwais port",
ETA: "2018-05-08",
JOINERS: "1",
OFFSIGNERS:"0" ,
};
Array1.push(postPayLoad);
var datavalue=JSON.stringify(Array1);
};
$.ajax({
url: '/S**********/***/N*****s/HusbandryRFQMultiple.xsjs',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(Array1),
dataType: 'json',
success: function(){alert("Success");},
error: function(jqXHR, textStatus, errorThrown){
sap.m.MessageBox.show(jqXHR.responseText, "ERROR", "Service call error"); }
});
Ajax call Method-2:
var jurl="/S**********/***/N*****s/HusbandryRFQMultiple.xsjs";
jQuery.ajax({
url: jurl,
async :false,
type: '{GET/POST}' ,
data:{dataobject:datavalue},
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic " + btoa("S******6"+":"+"U*******7")); },
method: 'POST',
dataType: 'json',
success: function(data) {
alert(data); console.log("Success!!!:"+data);
},
error: function (xhr,ajaxOptions,throwError){
alert("Errror");}, });
But I get only error response. Am I missing something? Can some one help me with this?
Regards,
Ramya
Request clarification before answering.
Hi all,
I was able to achieve post call using xsjs service after removing the data type from the ajax call. I have also changed some parameters in the ajax call. Below code is working for me.
var jurl="https://***************/********/HusbandryRFQMultiple.xsjs";
jQuery.ajax({
url: jurl,
type: 'POST' ,
data:datavalue,
method: 'POST',
username:"S***********",
password:"********07",
xhrFields : {
withCredentials : true
},
success: function(data,oResponse) {
console.log(JSON.stringify(data));
},
error: function (xhr,ajaxOptions,throwError){
console.log(throwError);
},
});
Reagrds,
Ramya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
check the network tab in F12, what does it say there?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ramya ,
Where did you implement the post method in XSJS .. Did you implemented it ??
Thanks
Viplove
Hi Viplove,
Yes we have implemented the Post method in XSJS. I am able to post the data from Rest Client. But it's not happening in WEB IDE.
$.response.headers.set("Access-Control-Allow-Origin", "*");
$.response.status = $.net.http.OK;
var body = $.request.body.asString();
var json = JSON.parse(body);
var conn = $.db.getConnection();
conn.prepareStatement("SET SCHEMA \"S0019363960\"").execute();
var st = conn.prepareStatement("INSERT INTO EPDAHUSBANDRY values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
for (var i = 0; i < json.length; i++){
st.setString(1,json[i].CUSTOMERID);
st.setString(2,json[i].CUSTOMERNAME);
st.setString(3,json[i].CUSTOMEREMAIL);
st.setString(4,json[i].SERVICENAME);
st.setString(5,json[i].VESSELNAME);
st.setString(6,json[i].PORTNAME);
st.setString(7,json[i].ETA);
st.setString(8,json[i].JOINERS);
st.setString(9,json[i].OFFSIGNERS);
st.setString(10,json[i].MODEOFPAYMENT);
st.setString(11,json[i].NOOFCREWSHORELEAVE);
st.setString(12,json[i].COMPLAINS);
st.setString(13,json[i].NOOFVISITORS);
st.setString(14,json[i].FROMVESSEL);
st.setString(15,json[i].TOVESSEL);
st.setString(16,json[i].PASSPORTNUM);
st.setString(17,json[i].CREW);
st.setString(18,json[i].NATIONALITY);
st.setString(19,json[i].COMMENTS);
st.setString(20,json[i].PERSONNAME);
st.setString(21,json[i].SEAMANBOOKNUMBER);
st.setString(22,json[i].TYPEOFCARGO);
st.setString(23,json[i].QTY);
st.setString(24,json[i].PACKINGLIST);
st.setString(25,json[i].TOTALVOLUME);
st.setString(26,json[i].TOTALWEIGHT);
st.setString(27,json[i].TYPEOFPACKAGING);
st.setString(28,json[i].REQUESTEDDATE);
st.setString(29,json[i].REQUESTEDTIME);
st.setString(30,json[i].NOOFPERSONS);
st.setString(31,json[i].PRIORITY);
st.setString(32,json[i].ATTACHFILE);
st.setString(33,json[i].FILENAME);
st.execute();
conn.commit();
}
As post requests need an X-CSRF-Token and you do not pass one you get the 405 error. You can check how to add one in the official documation or in step 3 of blog post https://blogs.sap.com/2016/03/29/developing-with-xs-advanced-add-authentication/ for instance. Consider that for the GET request which fetches the X-CSRF-Token you have to provide a valid GET endpoint in your API.
Regards,
Florian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 11 | |
| 6 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.