Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 访问控制允许原始标头不存在于fetch api调用中_Ajax_Go_Same Origin Policy - Fatal编程技术网

Ajax 访问控制允许原始标头不存在于fetch api调用中

Ajax 访问控制允许原始标头不存在于fetch api调用中,ajax,go,same-origin-policy,Ajax,Go,Same Origin Policy,所以我试着使用同构提取 我有一个用go编写的服务器,它将返回JSON数据。这就是我打电话的方式- export function fetchDistricts(geoState) { return function (dispatch) { dispatch(requestDistricts(geoState)); return fetch(`http://localhost:8100/districts/`) .then(r

所以我试着使用同构提取

我有一个用go编写的服务器,它将返回JSON数据。这就是我打电话的方式-

export function fetchDistricts(geoState) {

    return function (dispatch) {
        dispatch(requestDistricts(geoState));


        return fetch(`http://localhost:8100/districts/`)
            .then(response => {console.log(response);})
            .then(json => {
                console.log("json");
            });
}
我在chrome控制台中得到这个错误

Fetch API cannot load http://localhost:8100/districts/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8200' is therefore not allowed access.
这很奇怪,因为在我的处理程序中,我正在这样做

func getDistricts(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/jsonp;charset=UTF-8")
    w.WriteHeader(http.StatusOK)
    w.Header().Set("Access-Control-Allow-Origin", "*")
    rows, err := db.Query("SELECT * from districts")
    //other code here
此外,这是可行的

var activitiesDfD =  $.ajax({
    url: "http://localhost:8100/district/1/activities",
    type: "GET",
    dataType: "json"
});

$.when(activitiesDfD).then(function(data, textStatus, jqXhr) {
为什么在使用fetchapi时会失败?我该如何解决这个问题

编辑-

我现在已经试过了

func getDistricts(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/jsonp;charset=UTF-8")
    w.Header().Set("Access-Control-Allow-Origin", r.Header.Get(`origin`))
    w.WriteHeader(http.StatusOK)    

合并以下两个建议-但错误是相同的

几乎所有的web浏览器都拒绝使用源代码
“*”
。因此,将
“*”
作为
访问控制允许源站
头发送会导致同一源站策略冲突

幸运的是,有一个解决办法。如果查看处理此问题的代码,它所做的是重新发送浏览器发送的
“origin”
标题。因此,要使
*
工作,您必须执行以下操作:

w.Header().Set("Access-Control-Allow-Origin", r.Header.Get(`origin`))

我最终使用了这个中间件,使一切正常工作

在jQuery示例中,您调用的是
localhost:81000
,在fetch-one中,您调用的是
localhost:8100
。这是问题中的一个输入错误,还是您试图查找的错误?这是一个输入错误-很抱歉-更改我不知道您的服务器是什么语言,但您是否在设置“访问控制允许源代码”之前调用
w.WriteHeader
?语言是golang,呃-我是w.WriteHeader(http.StatusOK)如果您重新排列它们以在
WriteHeader
之前设置标题,它可能会起作用。事实上,在写入套接字后,您正在设置对象上的头,因此头不会被发送。这改变了错误,我现在有一个net::ERR\u SSL\u PROTOCOL\u错误-为什么会发生这种情况?好的,我尝试了新版本,现在得到了以前的错误。您可以在没有https的情况下尝试吗?SSL错误与CORS问题无关,是我做的。但我回到了“无法加载Fetch API。请求的资源上不存在“访问控制允许源代码”标头。因此不允许源代码“”访问。”执行r.header后出错。Get('Origin')之前是否需要添加标头?如果该标题不存在。