我正在使用 fetch 发送 post 请求。我正在使用 jsonbody 发送。它正在与邮递员一起使用,但不在我的反应代码中。请帮助
我已经在标头中设置了请求主体。我不知道为什么我收到 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
可以分享一下
handleSubmit()
功能吗?
我在学习 React 时也遇到了同样的问题,所以我在我的例子中所做的就是在我的
http://localhost:<port>
中添加 package.json
作为代理变量。
{...}
proxy: "http://localhost:3000",
{...}
就像你一样,我尝试添加 no-cors 但在我的情况下不起作用。所以我必须执行上述步骤。我也是初学者,所以我可能不对,但我的猜测是,fetch api 不喜欢 url 中的 localhost。