我正在尝试在 Jquery 中进行 ajax 调用,但收到空响应。但是当我尝试通过 curl 做同样的事情时,我成功了。这是我的JS,
time = new Date($.now());
requestJSON = '{"Method":"GET","AppName":"Proline","ServiceURL":"http://localhost:8081/api/services/tags/","Properties":null,"Object":"","Timestamp":"'+time+'"}'
$.ajax({
type: "GET",
url: "http://localhost:8081/api/services/tags/",
// The key needs to match your method's input parameter (case-sensitive).
data: requestJSON,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
我也试过,dataType: "jsonp"但没有运气。
curl 命令在这里,
curl --data '{"Method":"GET","AppName":"Proline","ServiceURL":"http://localhost:8081/api/services/tags/","Properties":null,"Object":"","Timestamp":"2016:03:27 00:08:11"}'
-X GET http://localhost:8081/api/services/tags/
我在 golang 中有服务器代码。我已经将标题设置为Access-Control-Allow-Headers:*. 这是我的服务器处理程序。
func tagsHandler(w http.ResponseWriter, r *http.Request, extra []string) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers",
"*")
}
// Stop here if its Preflighted OPTIONS request
if r.Method == "OPTIONS" {
return
}
var response []byte
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("FATAL IO reader issue %s ", err.Error())
}
具体来说,我在服务器端收到空请求正文。有人可以帮我吗?
繁星淼淼
HUH函数
相关分类