Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy not working #3557

Closed
sunnyYanan opened this issue Dec 6, 2017 · 5 comments
Closed

proxy not working #3557

sunnyYanan opened this issue Dec 6, 2017 · 5 comments

Comments

@sunnyYanan
Copy link

sunnyYanan commented Dec 6, 2017

I nearly create a project using create-react-app.
I set a proxy in package.json just like "proxy":"https://xx.com"

then I write a button on a page and do the fetch action, here is my code:

class App extends Component {
  constructor(props) {
    super(props);
    this.fetch = this.fetch.bind(this);
  }
  fetch() {
    fetch('/getPoplayer.json',{headers:{
      "Accept":"text/html, application/xhtml+xml, application/json, */*"
    }})
    .then(function(response) {
      return response.text()
    }).then(function(json) {
      console.log('parsed json', JSON.parse(json))
    }).catch(function(ex) {
      console.log('parsing failed', ex)
    })
  }
  render() {
    return (
      <div className="App">
        <Button onClick={this.fetch}>测试fetch</Button>
      </div>
    );
  }
}

export default App;

after executing 'npm start' , the request is like this

image

the proxy not works , but I console.log the proxySetting in start.js , it is right.

@Timer
Copy link
Contributor

Timer commented Dec 8, 2017

Proxy does not work for this usecase. Please call the URL directly in your application.

If they do not have CORS setup to allow this, you'll need to create your own server which reverse proxies to that domain and set your own server in the proxy field.

@quanzhiyuan
Copy link

@Timer why not proxy does not work for this usecase ?

@Gribbs
Copy link

Gribbs commented Jan 22, 2018

so yeah, why does proxy not work here?

@gaearon
Copy link
Contributor

gaearon commented Jan 22, 2018

Because when text/html is in the Accept header there's no way our development server could guess whether you're requesting that URL through fetch or through the address bar. The second use case is important for client-side routing.

@devssh
Copy link

devssh commented Apr 27, 2018

I am not able to get proxy to work in application/json either.

@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@Timer @gaearon @sunnyYanan @Gribbs @devssh @quanzhiyuan and others