Skip to Content

CORS Issue with Chrome

Hello Experts,

I am trying to invoke Api.ai URL from my UI5 application using AJAX call, code is as follows :

onSearch : function (oEvent) {

var accessToken = "My Access Token";

var messageObject = {};
var that = this;
var baseUrl = "https://api.api.ai/v1/";
var sQuery = oEvent.getSource().getValue();
var sApiResSuccess;
var that=this;
var searchResult = this.getView().byId('productInput1');
$.ajax({
    type: "POST",
    url: baseUrl + "query",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    crossDomain: true,
    headers: {
      "Authorization": "Bearer " + accessToken,
    },
    data: JSON.stringify({query: sQuery, lang: "en", sessionId: "runbarry"}),
    success: function(data) {
      sApiResSuccess=data.result.speech;
      that._showResult(searchResult,sApiResSuccess);
      }
    },
    error: function() {
      respond(messageInternalError);
    }
  });
},


I have deployed this app on our gateway server and configured the same on our Fiori Launchpad. When I run the standalone BSP application on gateway server , it works fine , I am able to invoke Api.ai ( https://api.api.ai/v1/) , however , when I run the application through launchpad , it gives me CORS Issue , which is - field X-XHR-Logon is not allowed by Access-Control-Allow-Headers in preflight response.

I tried all possible options however not able to resolve this, could you please provide any inputs here ?

Hint : When I installed CORS extension of chrome and enabled it , it worked fine on my launchpad as well however I need to run this app on tablet browser where i can not add any extension.

Any help here would be highly appreciated.

Thanks,

Mahesh Z.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jan 09, 2017 at 01:09 PM

    You would want to add the service in gateway so all browser traffic is provided from gateway which would eliminate the error. See

    https://blogs.sap.com/2013/10/09/how-to-consume-an-odata-service-using-odata-services-consumption-and-integration-osci/

    Regards,

    Jamie

    SAP - Technology RIG

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 09, 2017 at 02:48 PM

    Hello Mahesh,

    Jamie's solution will work for you and your scenario (FLP on ABAP gateway). If you want to understand in general why this error occurs you can have a look at the walkthrough tutorial step 26 (scroll down to the middle to find CORS):

    https://sapui5.hana.ondemand.com/#docs/guide/44062441f3bd4c67a4f665ae362d1109.html

    For the different scenarios (FLP on HCP, FLP on Portal and FLP on ABAP) similar solutions to proxy requests exist.

    Regards, Annette

    Add comment
    10|10000 characters needed characters exceeded