V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
abbenyyy
V2EX  ›  程序员

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

  •  
  •   abbenyyy · 2017-12-07 14:04:03 +08:00 · 3879 次点击
    这是一个创建于 2304 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,我是 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 条回复    2017-12-07 14:30:47 +08:00
    sensui7
        1
    sensui7  
       2017-12-07 14:10:01 +08:00   ❤️ 1
    当然不用了, 把不变的部分保存起来,api.base({base: https://api.github.com, headers: {contentType:...}....)

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

    类似这样。

    2. 那样写是没问题的, 但要注意跨域的问题。
    wotemelon
        2
    wotemelon  
       2017-12-07 14:25:11 +08:00
    axios.createService({
    baseUrl: ''
    })
    chairuosen
        3
    chairuosen  
       2017-12-07 14:30:47 +08:00
    补充一点,前面那个 base 要写到 config 里,这样不同环境只改 config 不改代码
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2880 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:05 · PVG 21:05 · LAX 06:05 · JFK 09:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.