dounianxie2058 2017-01-30 15:16
浏览 56

有一个可以接收帖子的go Web服务,但不清楚如何将响应发送回jquery

Morning,

I have some go code that receives a json post and does some stuff, but I want to send back to jquery the result or a message.

package main

import (
    "fmt"
    "log"
    "net/http"
    "encoding/json"
    "os/exec"
    "strings"
)
type Deploy struct {
    Environmentid string `json:"environmentid"`
    Versionid string `json:"versionid"`
    Dtp string `json:"dtp"`
}
func deploy(rw http.ResponseWriter, req *http.Request) {
    if origin := req.Header.Get("Origin"); origin != "" {
        rw.Header().Set("Access-Control-Allow-Origin", origin)
        rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS,     PUT, DELETE")
        rw.Header().Set("Access-Control-Allow-Headers",
            "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-    Token, Authorization")
    }
    // Stop here if its Preflighted OPTIONS request
    if req.Method == "OPTIONS" {
        return
    }
    if req.Method == "POST" {
        d := Deploy{}
        err := json.NewDecoder(req.Body).Decode(&d)
        if err != nil {
            panic("Can't decode Json received in post")
        }
        fmt.Println("Deploy: " + d.Environmentid + "," + d.Versionid + "," +   d.Dtp)
        // Run command
        cmd := exec.Command("/bin/myscript.sh", strings.Split(fmt.Sprintf("-e %s -v %s", d.Environmentid, d.Versionid), " ")...)
        cmdOut, cmdErr := cmd.Output()
        if cmdErr != nil {
                panic(cmdErr)
        }
        fmt.Println(string(cmdOut))
    }

The jquery I have right now:

 $.ajax({
     type: 'POST',
     url: url,
     cache:false,
     crossDomain: true,
     contentType: 'application/json; charset=utf-8',
     data: jsonData,
               dataType: 'json',
     success:function(result) {
           $('#target').html('Request was sent successfully...');
           console.log(result);
      },
      error:function(xhr,status,error) {
           //alert(error);
      }
 });

I see the 3 things I send just fine.
I think I saw some reference to some option called callback for the $.ajax. I am guessing that that allows me to put a function name to receive a response. An example of how to receive a json response that has the success or error from the go server would be appreciated, and how to send the response from the go server to this callback.

  • 写回答

3条回答 默认 最新

  • douxianji6104 2017-01-30 15:26
    关注

    In your ajax call you've already specified a callback to receive a response; that's what success is, and it will receive the data provided by the server's http.ResponseWriter.

    If what you mean is that after the http request/response cycle is complete, at some later time you want the server to initiate a message to the client, then you need to be looking at websockets.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)