所以我有这个Go http处理程序,将一些POST内容存储到数据存储中,并检索一些其他信息。在后端我用:
func handleMessageQueue(w http.ResponseWriter,r *http.Request) { w.Header().Set("Access-Control-Allow-Origin","*") if r.Method == "POST" { c := appengine.NewContext(r) body,_ := IoUtil.ReadAll(r.Body) auth := string(body[:]) r.Body.Close() q := datastore.NewQuery("Message").Order("-Date") var msg []Message key,err := q.GetAll(c,&msg) if err != nil { c.Errorf("fetching msg: %v",err) return } w.Header().Set("Content-Type","application/json") jsonMsg,err := json.Marshal(msg) msgstr := string(jsonMsg) fmt.Fprint(w,msgstr) return } }
在我的firefox OS应用程序中,我使用:@H_404_4@
var message = "content"; request = new XMLHttpRequest(); request.open('POST','http://localhost:8080/msgs',true); request.onload = function () { if (request.status >= 200 && request.status < 400) { // Success! data = JSON.parse(request.responseText); console.log(data); } else { // We reached our target server,but it returned an error console.log("server error"); } }; request.onerror = function () { // There was a connection error of some sort console.log("connection error"); }; request.send(message);
传入的部分都沿着这样的方向工作。但是,我的回复正在被阻止。给我以下消息:@H_404_4@
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/msgs. This can be fixed by moving the resource to the same domain or enabling CORS.
我尝试了很多其他的事情,但是我无法从服务器得到响应。但是,当我将Go POST方法更改为GET并通过浏览器访问该页面时,我会收到我想要的数据。我不能确定哪一方出错,为什么可能是Go不应该阻止这些请求,但也可能是我的javascript是非法的。@H_404_4@
@Egidius在创建XMLHttpRequest时应该使用
var xhr = new XMLHttpRequest({mozSystem: true});
什么是mozSystem?@H_404_4@
mozSystem Boolean:将此标志设置为true允许进行跨站点连接,而不需要服务器选择使用CORS。需要设置mozAnon:true,即不能与发送Cookie或其他用户凭据相结合。这只适用于特权(已审核)的应用程序;它不适用于Firefox中加载的任意网页。@H_404_4@
更改你的清单@H_404_4@
在您的清单上,不要忘记将此行包含在您的权限中:@H_404_4@
"permissions": { "systemXHR" : {},}