如何使用访问控制允许来源?它只是在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头标签之间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐