[英]React js fetch api show different behaviour
我使用fetch API進行服務器調用,除了一個 post 請求之外,所有另一個 post/get 請求都在完全/欲望服務器 URL 上命中,但一個 post 請求在 localhost 服務器上命中。
用於發布請求的服務 class 代碼和基礎 URL 與其他服務相同。
service.js:56 POST http://localhost:3000/ .... 500(內部服務器錯誤)
代碼:
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;
});
}
...點擊本地主機...
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"
},
您可以將服務器 url 僅沒有 api 版本前綴放在package.json
文件中。 這可以解決問題,請確保您處於開發模式。
您在服務器端是否有正確的/phases
位置路由?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.