如何使用访问控制允许来源?它只是在html头标签之间吗?

How do I use Access-Control-Allow-Origin? Does it just go in between the html head tags?(如何使用访问控制允许来源?它只是在html头标签之间吗?)
本文介绍了如何使用访问控制允许来源?它只是在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头标签之间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

document.write() overwriting the document?(document.write() 覆盖文档?)
IE10 and Cross-origin resource sharing (CORS) issues with Image / Canvas(IE10 和 Image/Canvas 的跨域资源共享 (CORS) 问题)
Importing script with type=module from local folder causes a CORS issue(从本地文件夹导入 type=module 的脚本会导致 CORS 问题)
Javascript CORS - No #39;Access-Control-Allow-Origin#39; header is present(Javascript CORS - 不存在“Access-Control-Allow-Origin标头)
HTML crossorigin attribute for img tag(img 标签的 HTML 跨域属性)
MediaElementAudioSource outputs zeros due to CORS access restrictions local mp3 file(由于 CORS 访问限制本地 mp3 文件,MediaElementAudioSource 输出零)