【发布时间】:2018-11-18 19:56:44
【问题描述】:
我使用 jquery 以 jsonp 格式发送一些数据:
console.log(data.location.lng);
console.log(data.location.lat);
console.log(data.accuracy);
var urlgr = "http://127.0.0.1:9220/geo/rec_geo/";
var pfg = {
//lo: data.location.lng,
lo: 1.0,
//la: data.location.lat,
la: 2.0,
//ac: data.accuracy,
ac: 3,
};
$.ajax({
type: 'GET',
url: urlgr,
crossDomain: true,
data: JSON.stringify(pfg),
dataType: 'jsonp',
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log("data rec geo");
console.log(data);
}
});
这是我在 golang 中接收它的代码:
type geoData struct {
Long float32 `json:"lo"`
Lat float32 `json:"la"`
Accuracy int `json:"ac"`
}
func recordGeo(w http.ResponseWriter, r *http.Request) {
s := strings.Split(r.RemoteAddr, ":")
ip := s[0]
Info.Println("4")
var geoRec geoData
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&geoRec)
fmt.Println("geoRec")
fmt.Println(geoRec)
checkErr(err)
}
但我有一个 EOF 错误:
2017/07/01 07:16:22 http: panic serving 127.0.0.1:50680: EOF
所以我的问题是如何在 golang 中正确接收 jsonp 请求并将其解析为结构?
这是我在 golang 服务器端收到的请求:
GET /geo/rec_geo/?callback=jQuery31108538596418163691_1498913991164&{%22lo%22:1,%22la%22:2,%22ac%22:3}&_=1498913991166 HTTP/1.1
Host: 127.0.0.1:9220
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
accept-encoding: gzip, deflate, sdch, br
accept-language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
referer: http://localhost:8080/
cookie: csrftoken=Y7WA181u22l9sScw9UbPxM38wCGQzjaLMMysAesJSWZm89Pj6CCdBkEF01ibpUjW
alexatoolbar-alx_ns_ph: AlexaToolbar/alx-4.0.1
connection: keep-alive
edit 删除了 : Info.Println 行 但我仍然有同样的错误
谢谢
【问题讨论】:
标签: javascript jquery go jsonp