简体   繁体   中英

React js fetch api show different behaviour

I make a server call using fetch API except one post request all another post/get request hit on the exactly/desire server URL, but the one post requests hit on the localhost server.

service class code and base URL for the post request is the same as others.

service.js:56 POST http://localhost:3000/ .... 500 (Internal Server Error)

Code:

function save(workflow) {
    const token = authenticationGenerator.generateAuthenticationToken(localStorage.getItem('username'),
        localStorage.getItem('password'));

    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': token },
        body: JSON.stringify(workflow)
    };
    return fetch('/workflow', requestOptions)
        .then(handleResponse).then(workflowData => {
            return workflowData;
        });
}

... hit on localhost...

function save(phase) {
    const token = authenticationGenerator.generateAuthenticationToken(localStorage.getItem('username'),
        localStorage.getItem('password'));
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': token },
        body: JSON.stringify(phase)
    };
    return fetch('/phases', requestOptions)
        .then(handleResponse).then(phaseData => {
            return phaseData;
        });
}

...package.json...

"scripts": {
    "start": "react-scripts start",
    "build": "webpack --mode production",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
},
"proxy": "http://url../v1",
"eslintConfig": {
    "extends": "react-app"
},

You can put server url only without api version prefix in package.json file. That could resolve the issue and please make sure you are in development mode.

Do you have correct routing for /phases location at the server end?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM