[英]Reactjs - TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL from ***.**.*.***:2000
This is my reactjs code and I am having problem with fetch.这是我的 reactjs 代码,我在获取时遇到问题。 I have created own api using nodejs and trying to retrieve some demo json using get method.我已经使用 nodejs 创建了自己的 api,并尝试使用 get 方法检索一些演示 json。 I am using same local IP for both reactjs and nodejs, yes i am using different port.我对 reactjs 和 nodejs 使用相同的本地 IP,是的,我使用了不同的端口。
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
class App extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
componentDidMount() {
fetch(' ***.**.*.***:2000', {
method: 'GET'
})
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result
});
console.log(result);
},
(error) => {
this.setState({
isLoaded: true,
error
});console.log(error)
}
)
}
render() {
return (
<ul>fdg
{/* {this.state.items.map(item => (
<li key={item.id}>
{item.name}
</li>
))}*/}
</ul>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
Error is shown after fetch is failed获取失败后显示错误
TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL
from ***.**.*.***:2000
at App.componentDidMount (index.js:15)
at commitLifeCycles (react-dom.development.js:14361)
at commitAllLifeCycles (react-dom.development.js:15462)
at HTMLUnknownElement.callCallback (react-dom.development.js:100)
at Object.invokeGuardedCallbackDev (react-dom.development.js:138)
at invokeGuardedCallback (react-dom.development.js:187)
at commitRoot (react-dom.development.js:15603)
at completeRoot (react-dom.development.js:16618)
at performWorkOnRoot (react-dom.development.js:16563)
at performWork (react-dom.development.js:16482)
at performSyncWork (react-dom.development.js:16454)
at requestWork (react-dom.development.js:16354)
at scheduleWork$1 (react-dom.development.js:16218)
at scheduleRootUpdate (react-dom.development.js:16785)
at updateContainerAtExpirationTime (react-dom.development.js:16812)
at updateContainer (react-dom.development.js:16839)
at ReactRoot../node_modules/react-dom/cjs/react-dom.development.js.ReactRoot.render
(react-dom.development.js:17122)
at react-dom.development.js:17262
at unbatchedUpdates (react-dom.development.js:16679)
at legacyRenderSubtreeIntoContainer (react-dom.development.js:17258)
at Object.render (react-dom.development.js:17317)
at Object../src/index.js (index.js:48)
at __webpack_require__ (bootstrap d08df1f09c74587853fb:678)
at fn (bootstrap d08df1f09c74587853fb:88)
at Object.0 (index.js:49)
at __webpack_require__ (bootstrap d08df1f09c74587853fb:678)
at ./node_modules/ansi-regex/index.js.module.exports (bootstrap d08df1f09c74587853fb:724)
at bootstrap d08df1f09c74587853fb:724
Here is is some update that i made to code.这是我对代码所做的一些更新。 First i made mistake is i didnt add http:// to my IP address during sending request.首先我犯的错误是我没有在发送请求期间将 http:// 添加到我的 IP 地址。 After doing that i got another error这样做之后,我又犯了另一个错误
Failed to load http:// .无法加载 http:// 。 . . . . :2000/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. :2000/: 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://来源 'http:// . . . . . . :2000' is therefore not allowed access. :2000' 因此不允许访问。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
Which means i am forgetting to add cors, Information about How to install and use cors inside your express node, can get from this link https://daveceddia.com/access-control-allow-origin-cors-errors-in-react-express/这意味着我忘记添加 cors,有关如何在您的 express 节点中安装和使用 cors 的信息,可以从此链接获取https://daveceddia.com/access-control-allow-origin-cors-errors-in-react -表达/
Accepted answer is the example of node express code.接受的答案是节点快递代码的示例。
This error is caused because of server side node express is incomplete. 由于服务器端节点表示不完整而导致此错误。 We need to add express().use("*",cors()) to express to work. 我们需要添加express()。use(“ *”,cors())进行工作。
Soln:- 溶液: -
var express = require('express');
var cors = require('cors');
var app = express().use("*",cors());
var router = app.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
// Comment out this line:
//res.send('respond with a resource');
// And insert something like this instead:
res.json([{
id: 1,
username: "samsepi0l"
}, {
id: 2,
username: "D0loresH4ze"
}]);
});
module.exports = router;
If anyone still has the error.如果有人仍然有错误。 this is my code这是我的代码
const client = new ApolloClient({
uri: 'http://localhost:4000:graphql',
});
I got the url wrong.我把网址弄错了。 Should be like this应该是这样的
const client = new ApolloClient({
uri: 'http://localhost:4000/graphql',
});
and if the error in the console is like this:如果控制台中的错误是这样的:
Failed to load http://...:2000/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.无法加载 http://...:2000/:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://...:2000' is therefore not allowed access. Origin 'http://...:2000' 因此不允许访问。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
Means you have to add cors in your root js意味着你必须在你的根 js 中添加 cors
const cors = require('cors');
const app = express();
// allow cors origin requests
app.use(cors());
Hope I helped.希望我有所帮助。 By the way this happened to me so maybe I thought I could help others who has this error.顺便说一句,这发生在我身上,所以也许我认为我可以帮助其他有此错误的人。 Take Care and Good Luck 😀😀😀保重,祝你好运😀😀😀
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.