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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序