预检响应在axios中没有HTTP正常状态

时间:2018-09-17 14:26:00

标签: javascript reactjs axios

我正在尝试使用开放式气象API添加站点,并使用axios进行发布请求

const stationData={
        external_id:this.state.external_id,
        name:this.state.name,
        latitude:this.state.latitude,
        longitude:this.state.longitude,
        altitude:this.state.altitude,
        }


    const url = `http://api.openweathermap.org/data/3.0/stations?appid=${ApiKey}`;

    var headers = {
        'Content-Type': 'application/json;charset=UTF-8', 
        "Access-Control-Allow-Origin": "*"
    }

    axios.post(url, data, {headers: headers}).then((response)=>{
console.log("response is " + response);
      }).catch((err)=>{
console.log("error is " + err);
      })

但这给我一个错误

OPTIONS http://api.openweathermap.org/data/3.0/stations?APPID=2747592557924542516e283a7f905a81 404 (Not Found)

Failed to load http://api.openweathermap.org/data/3.0/stations?APPID=2747592557924542516e283a7f905a81: Response for preflight does not have HTTP ok status.

error is Error: Network Error

如果我使用邮递员发送发帖请求,该请求在那儿工作,请帮忙吗?

1 个答案:

答案 0 :(得分:0)

从Axios Git:Unfortunately, I think this boils down to one of the dreaded CORS issues that developers often face. I could duplicate this and I tried a bunch of ways to poke at it to get the preflight request to work, but I couldn't find an opening in the server to let me through

其他有用的链接:cannot get cross-site POST to workcan not make post request ( cross domain ) 。基本上,这是浏览器安全问题,而不是Axios。他们不打算解决此问题:您选择另一个库或从后端处理它。请注意,在Chrome中禁用安全选项时,请求有效。