所以我试图使用 isomorphic-fetch https://www.npmjs.com/package/isomorphic-fetch我有一个用 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) {为什么在使用 fetch API 时会失败,我该如何解决这个问题?编辑-我现在试过这个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) 结合下面的两个建议 - 但错误是一样的。
- 2 回答
- 0 关注
- 280 浏览
添加回答
举报
0/150
提交
取消