3
votes

I am using react with redux and axios for async actions. And laravel 5.2 api for backend. React is in actual domain and api is in subdomain. When I try to call async get request to the api I am getting 200 Ok from network but getting error on console.

Console Log :

XMLHttpRequest cannot load http://api.doublecurlybraces.me/api/test?api_token=inpm38XbI9Fof7CZv99VlfNQTjx02EjaL5V1B3########### . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://doublecurlybraces.me' is therefore not allowed access.

Network : 
--General--
Request Method:OPTIONS
Status Code:200 OK
Remote Address:46.101.###.###:80
----
--Response Header--
Allow:GET,HEAD,POST
Cache-Control:no-cache
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Wed, 10 Aug 2016 15:08:01 GMT
Server:nginx/1.11.1
Transfer-Encoding:chunked
--Request Header--
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, access-control-allow-headers, access-control-allow-origin, x-requested-with
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:api.doublecurlybraces.me
Origin:http://doublecurlybraces.me
Referer:http://doublecurlybraces.me/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
#

Action Code

import axios from "axios";

var config = {
       headers: {
             'Content-Type': 'application/json',
             'Access-Control-Allow-Headers':'*',
             'Access-Control-Allow-Origin' : 'http://doublecurlybraces.me',
             'X-Requested-With': 'XMLHttpRequest'
       },
       params: {
             api_token : 'inpm38XbI9Fof7CZv99VlfNQTjx02EjaL#############',

       },

};

export function stockChanges(){

    return function(dispatch){
        axios.get('http://api.doublecurlybraces.me/api/test',config)
          .then(function (response) {
            dispatch({type : "FETCH_STOCK_FULFILLED" , payload : response.data})
          })
          .catch(function (error) {
            dispatch({type : "FETCH_STOCK_REJECTED" , payload : error})
          });
    }
}
1

1 Answers

1
votes

The problem is that the server does not return Access-Control-Allow-Origin header.

Access-Control-Allow-Origin: http://doublecurlybraces.me

Please note that Access-Control-Allow-Origin is a response header, so you should not add it to the axios request config.