How do I use Access-Control-Allow-Origin? Does it just go in between the html head tags?(如何使用访问控制允许来源?它只是在html头标签之间吗?)
问题描述
我一直在阅读有关 Access-Control-Allow-Origin
的内容,因为它似乎可以有效地允许跨域请求,因为我可以访问外部站点.我的问题是如何使用 Access-Control-Allow-Origin
来允许跨域请求.我试过这个(别笑)(顺便说一句,我想要一个数字,返回 1 或 0)
I've been reading about Access-Control-Allow-Origin
because it seems effective at allowing cross domain requests since I have access to the external site. My question ism how do I use Access-Control-Allow-Origin
to allow cross domain requests. I tried this (don't laugh) (by the way all I want is for a single number, 1 or 0 to be returned)
<html>
<head>
Access-Control-Allow-Origin: *
</head>
<body>
1
</body>
</html>
我接近了吗?谢谢你的帮助.如果有更简单的方法来执行简单的跨域请求,请告诉我.
Am I close? Thanks for your help. If there is an easier way to do a simple cross-domain request let me know.
推荐答案
这是一个HTTP头.您将配置您的网络服务器或网络应用程序以理想地发送此标头.也许在 htaccess 或 PHP 中.
That is an HTTP header. You would configure your webserver or webapp to send this header ideally. Perhaps in htaccess or PHP.
或者你也可以使用
Alternatively you might be able to use
<head>...<meta http-equiv="Access-Control-Allow-Origin" content="*">...</head>
我不知道这是否可行.并非所有 HTTP 标头都可以直接在 HTML 中配置.
I do not know if that would work. Not all HTTP headers can be configured directly in the HTML.
这可以替代许多 HTTP 标头,但请参阅下面的 @EricLaw 的评论.这个特定的标头是不同的.
This works as an alternative to many HTTP headers, but see @EricLaw's comment below. This particular header is different.
警告
这个答案是关于如何设置标题的.我对允许跨域请求一无所知.
This answer is strictly about how to set headers. I do not know anything about allowing cross domain requests.
关于 HTTP 标头
每个请求和响应都有标头.浏览器将其发送到网络服务器
Every request and response has headers. The browser sends this to the webserver
GET /index.htm HTTP/1.1
然后是标题
Host: www.example.com
User-Agent: (Browser/OS name and version information)
.. Additional headers indicating supported compression types and content types and other info
然后服务器发送响应
Content-type: text/html
Content-length: (number of bytes in file (optional))
Date: (server clock)
Server: (Webserver name and version information)
可以配置其他头文件,例如 Cache-Control
,这完全取决于您的语言(PHP、CGI、Java、htaccess)和网络服务器(Apache 等).
Additional headers can be configured for example Cache-Control
, it all depends on your language (PHP, CGI, Java, htaccess) and webserver (Apache, etc).
这篇关于如何使用访问控制允许来源?它只是在html头标签之间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用访问控制允许来源?它只是在html头标签之间吗?
基础教程推荐
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01