I am trying to make an ajax call in Jquery but I am getting empty response. But when I try to do the same through curl I am succeeding. Here is my 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);
}
});
I also tried dataType: "jsonp"
but no luck.
And the curl command is here,
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/
I have the server code in golang. I already set the header to Access-Control-Allow-Headers:*
. Here is my server handler.
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)
}
To be specific I am getting empty request body in server side. could someone help me with this?