cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to call XSJS service in WEBIDE project?

former_member195820
Participant
0 Likes
2,361

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?

error.png

Regards,

Ramya

Accepted Solutions (1)

Accepted Solutions (1)

former_member195820
Participant
0 Likes

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

Answers (2)

Answers (2)

brian_keenan
Product and Topic Expert
Product and Topic Expert

check the network tab in F12, what does it say there?

former_member195820
Participant
0 Likes

Hi Brain,

Thanks for your quick response.

Here's the screenshot of my network tab.

brian_keenan
Product and Topic Expert
Product and Topic Expert
0 Likes

ok so it is saying that 405 not allowed. The server doesnt seem to accept POST request on this xsjs. Are you able to make GET requests? check the .xsaccess file on HANA

former_member195820
Participant
0 Likes

I am not able to create .xsaccess file in WEB IDE Full Stack. If I give type as 'GET' I am getting 400 Bad Request.

Since ,I have created the application in WEB IDE I am not able to create the .xsaccess file here unlike HANA Editor.

former_member340030
Contributor

Hi Ramya ,

Where did you implement the post method in XSJS .. Did you implemented it ??

Thanks

Viplove

former_member195820
Participant
0 Likes

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();
} 



former_member340030
Contributor
0 Likes

Ok Did you add it as the destination and mention in the neo app json file of your Ui5 app ..

And also before posting you need to make a GET call to get X-CSRF token ..

Thanks

Viplove

pfefferf
Active Contributor
0 Likes

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