ES6 fetch:如何更改它调用的localhost端口?

时间:2017-11-27 10:49:41

标签: javascript spring ecmascript-6 fetch-api

我有一个webpack-dev服务器在:8080上运行node.js服务我的前端。我在:8088上运行了一个Spring Boot MVC应用程序,为我的实际后端服务提供服务。

我有一个我想要呼叫的登录系统:8088,每次该功能时,我如何更改fetch功能的永久设置以引用:8088而不是:8080被称为?

我尝试了以下方法,但无济于事:

login ({commit}, authData) {
      fetch('/api/login', {
        username: authData.username,
        password: authData.password
      }, {
        mode: 'no-cors',
        method: 'post',
        url: `http://localhost:8088`,
        credentials: 'include'
      }) // ... goes on 

但是,它仍然指的是8080:

bodyUsed: false
headers: Headers {  }
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:8080/api/login"

1 个答案:

答案 0 :(得分:3)

您是否尝试将完整网址放入fetch first参数?非常确定/api/login会向当前主机发出请求。

fetch('http://localhost:8088/api/login', {
    username: authData.username,
    password: authData.password
  }, {
    mode: 'no-cors',
    method: 'post',
    url: `http://localhost:8088`,
    credentials: 'include'
  })

当然,您需要启用CORS才能正常工作。