cancel
Showing results for 
Search instead for 
Did you mean: 

How to post data to oData in sapui5(Insert)?

padma_mundru
Participant
0 Kudos

Hi All,

I need to insert the data in Database through OData model.Can you please help me?

Below is my code:

submitLineItem : function() {

  var grdata = new Object();

  OData

  .request(

  {

  requestUri : "http://host:8000/sap/opu/odata/sap/ZMM_GR_APP_SRV_01/PO_ItemSet(Ebeln=%273005000274%27)/PO_ItemSet",

  method : "GET",

  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'];

  console.log(data.results);

  grdata.material = data.results.Matnr;

  grdata.plant = data.results.Werks;

  grdata.stgeloc = data.results.Lgort;

  grdata.vendor = data.results.Emlif;

  grdata.ponumber = data.results.Ebeln;

  grdata.poItem = data.results.Ebelp;

  console.log(header_xcsrf_token);

  OData

  .request(

  {

  requestUri : "http://host:8000/sap/opu/odata/sap/ZMM_GR_APP_SRV_01/GRSet",

  method : "POST",

  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 : {

  GmCode : '01',

  Id : 00001,

  Number : 222,

  PstngDate : '1434634546',

  DocDate : '1147046400000',

  RefDocNo : '0001',

  Material : grdata.material,//getting from response

  Plant : grdata.plant,//getting from response

  StgeLoc : LineUpdatedData.storageLoc,//getting from response

  MoveType : '101',

  Vendor : grdata.vendor,//getting from response

  EntryQnt : LineUpdatedData.quantity,//getting from response

  EntryUom : 001,

  PoNumber : grdata.ponumber,//getting from response

  PoItem : grdata.poItem,//getting from response

  MvtInd : 'x'

  }

  },

  function(data,

  response) {

  alert("success");

  cosole

  .log(data);

  cosole

  .log(response);

  },

  function(err) {

  console

  .log(err);

  alert(err);

  });

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

  });

  },

When I am using above code, I am getting Bad request error

Please resolve this error.

Thanks in advance.

Accepted Solutions (0)

Answers (2)

Answers (2)

Private_Member_15166
Active Contributor
0 Kudos

Check for the error you got while running this transaction.

/IWFND/error_log in SAP system.

santhu_gowdaz
Active Contributor
0 Kudos

your code is fine. what is the issue?

why you are hard coded to post? and what about grdata?

400 bad request -

1. filed to sent some required data to back end.

2. date is not sending proper manner.

padma_mundru
Participant
0 Kudos

Hi Santhosh,

Thanks for your response,

I am getting the resonse data from OData and store it in 'grdata' object.

passing that into post request.

santhu_gowdaz
Active Contributor
0 Kudos

may be wrong way, then how you will get   "X-CSRF-Token".

put break point and check line by line. which line you are getting error. and check grdata is having some data.