最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6 fetch: How do I change the localhost port it calls? - Stack Overflow

programmeradmin3浏览0评论

I have a webpack-dev-server running with node.js on :8080 serving my frontend. I have a Spring Boot MVC application running on :8088 serving my actual backend service.

I have a login system I want to call on :8088, how would I change the permanent settings of the fetch function to refer to :8088 instead of :8080 every time the function is called?

I've tried the following, but to no avail:

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 

However, it still refers to 8080:

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

I have a webpack-dev-server running with node.js on :8080 serving my frontend. I have a Spring Boot MVC application running on :8088 serving my actual backend service.

I have a login system I want to call on :8088, how would I change the permanent settings of the fetch function to refer to :8088 instead of :8080 every time the function is called?

I've tried the following, but to no avail:

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 

However, it still refers to 8080:

bodyUsed: false
headers: Headers {  }
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:8080/api/login"
Share Improve this question asked Nov 27, 2017 at 10:49 cbllcbll 7,21929 gold badges78 silver badges123 bronze badges 3
  • u can change it from application.properties – Balasubramanian Commented Nov 27, 2017 at 10:51
  • Could you provide an example of configuring it? – cbll Commented Nov 27, 2017 at 10:51
  • It would be convenient to be able to set the port for fetch calls so that you can test on the same URL like you are describing. I am in the same situation – 1.21 gigawatts Commented Jul 26, 2024 at 21:41
Add a comment  | 

1 Answer 1

Reset to default 14

Have you tried to put full url in your fetch first parameter? Pretty sure /api/login would make the request to the current host.

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

Of course you need to have CORS enabled for this to work.

发布评论

评论列表(0)

  1. 暂无评论