[英]HTTP Requesting data from a local server
I'm running two Flask Apps on my local machine. 我在本地计算机上运行两个Flask Apps。
1) My test client website (on port 5001) 1)我的测试客户网站(在端口5001上)
2) My API (on port 5000) 2)我的API(在端口5000上)
If I type 127.0.0.1:5000/search/
into my browser, I get the following: 如果在浏览器中键入
127.0.0.1:5000/search/
得到以下信息:
Test Hello World
If I type 127.0.0.1:5001
into my browser I get an ordinary web page containing the following HTML code (this all looks OK): 如果我在浏览器中键入
127.0.0.1:5001
我会得到一个包含以下HTML代码的普通网页(一切看起来都不错):
<!DOCTYPE html>
<html>
<body>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo">TEST</p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "127.0.0.1:5000/search/", true);
xhttp.send();
}
</script>
</body>
</html>
As you can see the HTTP request is sent to 127.0.0.1:5000/search/
which is tested and working in the browser and should return Test Hello World
but unfortunately nothing happens. 如您所见,HTTP请求已发送到
127.0.0.1:5000/search/
,该请求已在浏览器中经过测试并且可以正常工作,并且应返回Test Hello World
但不幸的是没有任何反应。
Basically, I click the "Request Data" button (see above), and absolutely nothing happens. 基本上,我单击“请求数据”按钮(请参见上文),并且绝对没有任何反应。
Any ideas why? 有什么想法吗? I've been stuck on this for hours.
我已经坚持了几个小时。
For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.
出于安全原因,浏览器限制了从脚本内部发起的跨域HTTP请求。 For example, XMLHttpRequest follows the same-origin policy.
例如,XMLHttpRequest遵循同源策略。 So, a web application using XMLHttpRequest could only make HTTP requests to its own domain.
因此,使用XMLHttpRequest的Web应用程序只能向其自己的域发出HTTP请求。 To improve web applications, developers asked browser vendors to allow XMLHttpRequest to make cross-domain requests.
为了改进Web应用程序,开发人员要求浏览器供应商允许XMLHttpRequest进行跨域请求。
These resources may be helpful; 这些资源可能会有所帮助;
HTTP access control (CORS) HTTP访问控制(CORS)
Using CORS 使用CORS
"127.0.0.1:5000/search/"
Your URL is missing its scheme. 您的网址缺少其方案。 You need to prefix that with
http://
. 您需要在其前面加上
http://
。
If you open your browser's developer tools and look at the Network tab, you should see a 404 error listed there. 如果打开浏览器的开发人员工具并查看“网络”选项卡,则应该在其中看到404错误。
Once you fix that, it is likely that you'll have a Cross Origin error as described in this question 解决此问题后,很可能会出现如本问题所述的跨源错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.