• 请不要在回答技术问题时复制粘贴 AI 生成的内容
abbenyyy
V2EX  ›  程序员

前后端分离就是前端每次与后端交互都要写完整的 API 吗?

  •  
  •   abbenyyy · Dec 7, 2017 · 4659 views
    This topic created in 3079 days ago, the information mentioned may be changed or developed.

    如题,我是 android 开发初学前端。

    例如https://api.github.com/users/${userName}/repos

    每一次与后端交互都写完整的 API: https://api.github.com/users/abbenyyyyyy/repos

    这是前后端分离,请问我的理解对吗?谢谢

    最后还有个问题,如果我用 SpringBoot 写后端,有接口 http://localhost:8080/hello 返回字符串 hello,在前端应该要怎么写 API 呢?直接 fetch('http://localhost:8080/hello')吗?

    3 replies    2017-12-07 14:30:47 +08:00
    sensui7
        1
    sensui7  
       Dec 7, 2017   ❤️ 1
    当然不用了, 把不变的部分保存起来,api.base({base: https://api.github.com, headers: {contentType:...}....)

    然后用 api.get('/users/alex')

    类似这样。

    2. 那样写是没问题的, 但要注意跨域的问题。
    wotemelon
        2
    wotemelon  
       Dec 7, 2017
    axios.createService({
    baseUrl: ''
    })
    chairuosen
        3
    chairuosen  
       Dec 7, 2017
    补充一点,前面那个 base 要写到 config 里,这样不同环境只改 config 不改代码
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5512 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 93ms · UTC 03:46 · PVG 11:46 · LAX 20:46 · JFK 23:46
    ♥ Do have faith in what you're doing.