dongxiong2000 2016-03-27 09:53
浏览 112
已采纳

如何使用Jquery和golang中的输入json数据进行Ajax调用?

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?

  • 写回答

2条回答 默认 最新

  • doufenyu7610 2016-03-27 13:39
    关注

    The issue was the call is not sending the request data in body [But somehow curl works fine]. When I try to get the body, err := ioutil.ReadAll(r.Body) it gave empty value. So I changed to get from r.Form

    r.ParseForm()
    var body []byte
    for key, _ := range r.Form {
        body = []byte(key)
        break
    }
    

    r.Form gives a map. So I am iterating through the map and getting the req data. This SO post gave the solution. May be helpful to others :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动