[英]What exactly should I use as an argument 'url' in http request if i run Node.js server locally?
I'm trying to make a simple http request to my local server (running by Node.js). 我正在尝试向我的本地服务器发送一个简单的http请求(由Node.js运行)。 How my needed url should looks like?
我需要的网址应该如何?
I'm trying to make my website behaves in a next way: After button click the script is run. 我正试图让我的网站以下一种方式运行:点击按钮后运行脚本。 Script forms http request and sends it to my Node server, which I run locally on my computer with command line.
脚本形成http请求并将其发送到我的节点服务器,我使用命令行在我的计算机上本地运行。 The Node server handles http request, fetches data from my mongodb database and responds with this data to my script.
节点服务器处理http请求,从我的mongodb数据库中获取数据,并将此数据响应到我的脚本。 Finally, the script must output the data as a content on a webpage.
最后,脚本必须将数据作为内容输出到网页上。 The problem is that I get this error in a browser console:
问题是我在浏览器控制台中收到此错误:
'Access to XMLHttpRequest at 'file:///C:/.../serverSide.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.'
'对来自'null'的'file:/// C:/.../ serverSide.js'访问XMLHttpRequest已被CORS策略阻止:仅对协议方案支持跨源请求:http,data,chrome, chrome-extension,https。'
I guess it is because of invalid url, which I used as an argument in my http request. 我想这是因为无效的url,我在http请求中用作参数。 All of my files (html, script and node serverside) are in the same folder.
我的所有文件(html,脚本和节点服务器端)都在同一个文件夹中。 I have already used following
url
values: 'serverSide.js'
, 'http://localhost:8080/'
, 'localhost:8080'
and all of them caused an error which is above. 我已经使用了以下
url
值: 'serverSide.js'
, 'http://localhost:8080/'
, 'localhost:8080'
,所有这些都导致了错误。 So, what should I use as url
? 那么,我应该使用什么作为
url
?
my script: 我的剧本:
function textOutput() {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if ( this.readyState == 4 && this.status == 200 ) {
document.getElementById('questionText').innerHTML =
this.responseText
}
};
xhttp.open('GET', 'serverSide.js', true);
xhttp.send();
};
here is part of my serverside which is handling the request: 这是我的服务器端处理请求的一部分:
...
http.createServer(function (req, res) {
ukrlitquestions.findOne({id: randomId(1, docsAmount)}, 'text',
function(err, result) {
if (err) console.log(err);
let quesText = result.text;
res.write(quesText);
console.log(quesText);
res.end();
});
}).listen(8080);
...
You'll need to point to the correct domain. 您需要指向正确的域。 Something like
http://localhost:3000/server-side.js
像
http://localhost:3000/server-side.js
What you are seeing is a CORS error. 你看到的是一个CORS错误。 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
The web browser sends an OPTION request before sending the real request. Web浏览器在发送实际请求之前发送OPTION请求。 The Url should be something like
http://localhost:8080/server-side.js
Url应该类似于
http://localhost:8080/server-side.js
Try adding the following to the server side: 尝试将以下内容添加到服务器端:
if (req.method === "OPTIONS") {
res.sendStatus(200);
}
打开http.open('GET',' http:// localhost:8080 / serverSide.js ',true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.