我正在使用fetch发送发布请求。我正在使用jsonbody发送。它正在与邮递员一起工作,但在我的reactcode中不起作用。请帮助
我在标头中设置了reqeust主体。我不知道为什么我得到404找不到异常。
这是我的代码
authenticate=(loginid,passw)=>
{
console.log(loginid,passw);
const url ="http://localhost:8443/v2/rest/authenticateuser";
var requestBody={"loginid":loginid,"passw":passw};
console.log(requestBody);
fetch("http://localhost:8443/v2/rest/authenticateuser",
{
method: 'POST',
mode: 'no-cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // no-referrer, *client
body:JSON.stringify(requestBody)
})
.then(res => res.json())
.then(
(result) => {
console.log(result);
this.props.login(result.islogin);
})
.catch((error) => {
this.setState({show:false});
window.alert("error",error);
console.log("authentication failed");
});
}
//handlesubmit as a handler for submit button
handleSubmit = e => {
e.preventDefault();
this.props.manageridupdate(e.target.elements.loginid.value);
this.authenticate(e.target.elements.loginid.value,e.target.elements.passw.value);
};
我收到此错误。
POST http://localhost:8443/v2/rest/authenticateuser 404 (Not Found)
App.authenticate @ App.js:19
App.handleSubmit @ App.js:49
callCallback @ react-dom.development.js:147
invokeGuardedCallbackDev @ react-dom.development.js:196
invokeGuardedCallback @ react-dom.development.js:250
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:265
executeDispatch @ react-dom.development.js:571
executeDispatchesInOrder @ react-dom.development.js:596
executeDispatchesAndRelease @ react-dom.development.js:695
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:704
forEachAccumulated @ react-dom.development.js:676
runEventsInBatch @ react-dom.development.js:844
runExtractedEventsInBatch @ react-dom.development.js:852
handleTopLevel @ react-dom.development.js:5029
batchedUpdates$1 @ react-dom.development.js:21463
batchedUpdates @ react-dom.development.js:2247
dispatchEvent @ react-dom.development.js:5109
(anonymous) @ react-dom.development.js:21520
unstable_runWithPriority @ scheduler.development.js:255
interactiveUpdates$1 @ react-dom.development.js:21519
interactiveUpdates @ react-dom.development.js:2268
dispatchInteractiveEvent @ react-dom.development.js:5085
答案 0 :(得分:0)
您也可以共享handleSubmit()
函数吗?
学习反应时,我也遇到同样的问题,所以我的情况是在http://localhost:<port>
中添加package.json
作为代理变量。
{...}
proxy: "http://localhost:3000",
{...}
就像您一样,我尝试添加无心跳,但在我的情况下却不起作用。因此,我必须执行上述步骤。我也是初学者,所以我可能不太对,但是我的猜测是,获取api不喜欢url中的localhost。