ES6 fetch: How do I change the localhost port it calls?

12,847

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.

Share:
12,847
cbll
Author by

cbll

Updated on June 25, 2022

Comments

  • cbll
    cbll almost 2 years

    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"