dsa5233 2017-11-28 23:54
浏览 328
已采纳

API调用后如何重定向

I have an angular front end app and a golang api. The angular app is built (and runs fine). It makes a few calls to the go api which makes calls to an external service. After the calls to the external service, I want the frontend to redirect to a success page. I can't seem to get that to work in my Go code.

This is what I have (and works except for the redirect):

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/gin-contrib/static"
    "net/http"
    "io/ioutil"
    "strings"
    "encoding/json"
    "strconv"
    "net/http/cookiejar"
)

const BASE_ENDPOINT = "https://external.host.com/"
const FIRST_CALL_ENDPOINT = BASE_ENDPOINT + "first"
const SECOND_CALL_ENDPOINT = BASE_ENDPOINT + "second"


func main() {
    // create an http client with a cookie jar
    cookieJar, _ := cookiejar.New(nil)
    client := &http.Client{
        Jar: cookieJar,
    }

    router := gin.Default()
    // this is the default route for the front end.  The angular app will handle
    // all non-api calls
    router.Use(static.Serve("/", static.LocalFile("../frontend/dist", true)))

    // Routes
    api := router.Group("/")
    {
        // get post calls to this endpoint
        api.POST("/my/first", func(c *gin.Context) {

            userData := c.PostForm("userData")
            rawTextBody := "data=" + userData
            resp, err := client.Post(FIRST_CALL_ENDPOINT, "text/plain", strings.NewReader(rawTextBody))
            if err != nil {
                panic(err)
            }
            body, _ := ioutil.ReadAll(resp.Body)
            var data map[string]interface{}
            jsonErr := json.Unmarshal([]byte(body), &data)
            if jsonErr != nil {
                panic(jsonErr)
            }
            // data comes back as a "float64", is converted to an int, then to a string
            var jobId = strconv.Itoa(int(data["curjobId"].(float64)))
            moreUserData := c.PostForm("moreUserData")
            rawTextBodyCat := "secondUserData=" + moreUserData + "&jobid=" + jobId
            _, secErr := client.Post(SECOND_CALL_ENDPOINT, "text/plain", strings.NewReader(rawTextBodyCat))
            if secErr != nil {
                panic(secErr)
            }
            // TODO: need "successfully submitted" page. This already exists in the frontend angular server
            // I can manually go to that page. ('/my/success')

            // what i've tried:
            //http.Redirect(w, r, "/my/success", http.StatusSeeOther)
            //http.RedirectHandler( "/my/success", http.StatusSeeOther)

        })
    }

    router.NoRoute(func(c *gin.Context) {
        c.File("../frontend/dist/index.html")
    })

    // Run server
    router.Run(":8899")
}

In my code comments, you can see I've tried http.Redirect(w, r, "/my/success", http.StatusSeeOther) but I don't know how to call it in this context. I am not setting up a route listener, but just trying to trigger a redirect on demand. Should this be handled by the angular app somehow? How can I tell the angular app "all done"?

  • 写回答

1条回答 默认 最新

  • dqbr37828 2017-11-29 01:08
    关注

    You can not redirect frontend client app with an API response. What you need to do is to handle this on the client side, and use your router to navigate to correct page.

    What API does:

    Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
    

    source

    Which is simple instruction for your angular client what to do. Lets say in your $http success callback you call $location.url('/my/success') , which would use the Angular router to "redirect" to '/my/success'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿