Итак, у меня есть обработчик 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 я использую:
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);
Входящая часть все работает и все такое. Однако мой ответ блокируется. Давая мне следующее сообщение:
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 незаконен.
If POST is used to send data to the server, the Content-Type of the data sent to the server with the HTTP POST request is one of application/x-www-form-urlencoded, multipart/form-data, or text/plain
- person Not_a_Golfer   schedule 13.03.2014