如何在Jquery和golang中使用输入的json数据进行ajax调用?
我试图在Jquery中调用ajax,但得到的是空响应。但当我尝试通过卷曲做同样的事情时,我成功了。这是我的JS如何在Jquery和golang中使用输入的json数据进行ajax调用?,jquery,json,ajax,curl,go,Jquery,Json,Ajax,Curl,Go,我试图在Jquery中调用ajax,但得到的是空响应。但当我尝试通过卷曲做同样的事情时,我成功了。这是我的JS time = new Date($.now()); requestJSON = '{"Method":"GET","AppName":"Proline","ServiceURL":"http://localhost:8081/api/services/tags/","Properties":null,"Object":"","Timestamp":"'+time+'"}' $.ajax
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);
}
});
我还尝试了数据类型:“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的服务器代码。我已经将标题设置为访问控制允许标题:
。这是我的服务器处理程序
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())
}
log.Printf("Body : %s ", string(body))
method := r.Method
log.Printf("tagsHandler() :: service method %s ", method)
if method == HTTP_VERB_POST {
response = tagslogic.PostTag(body)
} else if method == HTTP_VERB_GET {
response = tagslogic.GetTags(body)
} else if method == HTTP_VERB_PUT {
response = tagslogic.PutTag(body)
} else if method == HTTP_VERB_DEL {
response = tagslogic.DelTag(body)
}
w.Write(response)
}
具体来说,我在服务器端得到了空的请求主体。
有人能帮我吗?您的数据已经在console.log中进行了字符串化
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/",
data:JSON.stringify(requestJSON),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
问题是调用没有在body中发送请求数据[但不知何故,
curl
工作正常]。当我试图获取主体时,err:=ioutil.ReadAll(r.body)
它给出了空值。所以我改成了从r.Form
r.ParseForm()
var body []byte
for key, _ := range r.Form {
body = []byte(key)
break
}
r.Form
给出了一张地图。因此,我遍历映射并获取req数据。这就给出了解决办法。可能对其他人有帮助:)我认为你不需要字符串化,你实际上是在字符串化2次,就像@Mir说的,requestJSON
已经是一个string@slash197-我尝试不使用JSON.stringify,但没有帮助:(@slash197-我更新了问题,因为它重定向到了JSON.stringify问题。我更改了JSON.stringify(requestJSON)要从更新代码中请求JSON和Shappawoo,您需要stringfy