Skip to Content
0

POST call for jQuery AJAX call

Sep 20, 2017 at 12:41 PM

347

avatar image
Former Member

Hi Experts,

I am making a POST call to xsjs service. I am not sure what is going wrong at the controller end, that I m unable to read the response.
When I open the service independently in a browser I can see the data but, through UI it gives an error Error 403 - Forbidden.
Could you please suggest where am I going wrong. Is it the XSJS service or in controller.
Appreciate your guidance on this.

XSJS-

var conn = $.hdb.getConnection();

var query,results;

try{

query = "select distinct CUST_NO,NAME,CNTRY_CODE from SCHEMA.TABLE_NAME ";

results = conn.executeQuery(query);

conn.commit();

var output = [[]];

for (var i = 0; i< results.length; i++)

{

output.push(results[i]);

}

output.shift();

conn.close();

$.response.setBody(JSON.stringify(output));

$.response.contentType = 'application/json';

$.response.status = $.net.http.OK; }

catch (e) {

$.response.status = $.net.http.INTERNAL_SERVER_ERROR; $.response.setBody(e.message);

}

AJAX call in CONTROLLER -

jQuery.ajax({

type : 'POST',

contentType: 'application/json',

url: "/z_demo1/services/cmsCurr.xsjs?action=Customer",

cache: false,

async : false,

beforeSend : function(jqXHR1,settings) {

jqXHR1.setRequestHeader('X-CSRF-Token',jqXHR1.getResponseHeader('X-CSRF-Token')); },

success: function(odata,response) {

alert("hi");

},

error: function(jqXHR1, textStatus, errorThrown) { alert(jqXHR1.responseText); }, dataType: "json"

}, this);

Thanks,

Supriya Sahu

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

avatar image
Former Member Sep 20, 2017 at 01:30 PM
0

Hi Supriya,

You can get the 403 - forbidden error when there is a problem in validating X-CSRF-Token from backend. Please check below thread:

https://archive.sap.com/discussions/thread/3804252

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Rahul,

Thanks for your reply. But, even after changing the "prevent_xsrf":false in .xsaccess file. I am still getting the same error.

Any other alternative ?

0
Former Member

Actually before making POST request, you have to send one GET request to backend to get the X-CSRF-Token and later use this token value while making POST request. Please see the below link for an example:

https://stackoverflow.com/questions/39375310/how-to-handle-x-csrf-token-for-jquery-post-in-ui5

0