dialogflow-javascript-client icon indicating copy to clipboard operation
dialogflow-javascript-client copied to clipboard

Access-Control-Allow-Origin issue

Open latheesh08 opened this issue 6 years ago • 6 comments

Failed to load https://api.api.ai/v1/query?v=20150910: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

Still having same issue please respond as fast as possible. if anybody have solutions please ping me,

thank you

latheesh08 avatar Oct 13 '18 10:10 latheesh08

change apiai.js this.uri = this.apiAiClient.getApiBaseUrl() + "query?v=" + this.apiAiClient.getApiVersion(); to this.uri = "https://cors-anywhere.herokuapp.com/" + this.apiAiClient.getApiBaseUrl() + "query?v=" + this.apiAiClient.getApiVersion();

this worked me

latheesh08 avatar Oct 13 '18 12:10 latheesh08

not sure using apps like https://cors-anywhere.herokuapp.com/ good solution for this issue.

TenTaeTme avatar Oct 14 '18 14:10 TenTaeTme

See here: you can install cors-anywhere on a server you own.

https://github.com/dialogflow/dialogflow-javascript-client/issues/102#issuecomment-429648436

fdavies93 avatar Oct 14 '18 18:10 fdavies93

I am also getting the same error. Plugin worked fine 3 days back but now this CORS issue has started to pop up. @fdavies93 - can you please tell how to include the cors anywhere in my nodejs code. THis is my node code:

var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var cors_proxy = require('cors-anywhere');

var app = express();

var port = process.env.PORT || 3000;
var ip = process.env.IP || '127.0.0.1';
var host = process.env.HOST || '0.0.0.0';

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  );
  res.header(
    'Access-Control-Allow-Methods',
    'GET, POST, PATCH, DELETE, OPTIONS'
  );
  next();
});

app.post('/', function(req, res) {
  console.log('REq body', req.body);
  console.log('Response ID', req.body.responseId);
  console.log('Action', req.body.queryResult.action);

let response = {
      text:
        'hey there',
      policies: ['abc', 'def'],
      context: 'report_claim_context'
    };

    res.json({
      payload: response
    });
}

app.listen(port,ip);

ATakaSKY avatar Oct 15 '18 13:10 ATakaSKY

I am also getting the same error. Plugin worked fine 3 days back but now this CORS issue has started to pop up. @fdavies93 - can you please tell how to include the cors anywhere in my nodejs code.

Maybe a spot of confusion here -- CORS anywhere is a proxy server which you'd have to run as a separate app, then route requests to api.api.ai through. Not sure about your setup but in a containerised environment this isn't too bad.

I did this by hunting down the URL variables in dialogflow-javascript-client, changing them to point at [proxy url]/https://api.api.ai/, then rebuilding the library for my apps. A bit of a pain but worked and was a quick fix until Google sort it properly.

I am curious why you're suddenly having CORS issues though, as your setup doesn't seem to touch the Dialogflow servers. Possibly you're not handling the OPTIONS call which Chrome (and maybe other browsers) sends before handling POST responses?

fdavies93 avatar Oct 15 '18 17:10 fdavies93

"Access to XMLHttpRequest at 'https://api.api.ai/v1/query?v=20150910' from origin 'https://.......com' has been blocked by CORS policy: Request header field x-usertoken is not allowed by Access-Control-Allow-Headers in preflight response."

I am getting this error, I allowed cord although

husnejahan avatar May 20 '19 13:05 husnejahan