A
A
Alexander Dmitriev2019-04-18 11:52:34
CORS
Alexander Dmitriev, 2019-04-18 11:52:34

External Ajax request not working (Laravel), Response to preflight request doesn't pass access control check?

I execute an Ajax request to an external service to get the exchange rate in json in Laravel:

$.ajax({
   url: 'https://www.cbr-xml-daily.ru/daily_json.js',
   type: 'GET',
   dataType: 'json',
   success: function(
      console.log(data);
    },
});

After sending the request, I get the following error:
Access to XMLHttpRequest at 'https://www.cbr-xml-daily.ru/daily_json.js?_token=arouoLREfMEpk7CypZFcYBPd9KyOCpJYPnI1qxZg' from origin 'http://mysite.exchange' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
irishmann, 2019-04-18
@s_asha1998dmit

Read about CORS Wikipedia
I also encountered such a problem, but I don’t have a backend, so I got out of the situation this way: I used a CORS proxy.

$.ajax({
  type:'POST',
  url: 'https://cors-anywhere.herokuapp.com/http://************.ru/api/',
  data: { 
    query: '{"ask":"'+message+'","userid":'+id+',"key":"1"}'
  },
  success: function(data){
    msg = JSON.parse(data);
    $(".messages").append('<li><div class="text-msg receive_msg">'+msg['aiml']+'</div></li>');
    while($(".messages li").length > 7){
      $('li:first').detach();
    }
  }
});

A
Anatoly, 2019-04-18
@AnatolTh

https://github.com/barryvdh/laravel-cors

A
Alexey, 2019-05-25
@alexkbs

Everything works as in the example from the question:

$.ajax({
  url: 'https://www.cbr-xml-daily.ru/daily_json.js',
  type: 'GET',
  dataType: 'json',
  success: function(data) {
    console.log(data.Valute.USD.Value.toFixed(4).replace('.', ','));
  },
});

Button demo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question