如何使用Access-Control-Allow-Origin?它只是进入html头标记之间? [英] How do I use Access-Control-Allow-Origin? Does it just go in between the html head tags?

查看:2064
本文介绍了如何使用Access-Control-Allow-Origin?它只是进入html头标记之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于 Access-Control-Allow-Origin 的信息,因为我可以访问外部网站,因为它似乎有效地允许跨域请求。我的问题ism如何使用 Access-Control-Allow-Origin 允许跨域请求。我试过这个(不要笑)(顺便说一句,我想要的是一个单一的数字,1或0被返回)

 < html> 
< head>
Access-Control-Allow-Origin:*
< / head>
< body>
1
< / body>
< / html>

我关闭?谢谢你的帮助。如果有更简单的方法来做一个简单的跨域请求让我知道。

解决方案

这是一个HTTP头。您可以配置Web服务器或Web应用程序以理想地发送此标头。也许在htaccess或PHP。



或者您也许可以使用

 code>< head> ...< meta http-equiv =Access-Control-Allow-Origincontent =*> ...< / head> 

我不知道这是否可行。不是所有的HTTP头都可以直接在HTML中配置。



这是一个替代许多HTTP头的方法,但看到@EricLaw 的评论。



这个答案完全是关于如何设置标题。我不知道允许跨网域请求的任何内容。



关于HTTP标头



每个请求和响应都有头。

  GET /index.htm HTTP / 1.1 

然后标题

 主机:www.example .com 
User-Agent:(浏览器/操作系统名称和版本信息)
..其他标题指示支持的压缩类型和内容类型和其他信息
pre>

然后服务器发送响应

  Content- text / html 
Content-length:(文件中的字节数(可选))
日期:(服务器时钟)
服务器:(Web服务器名称和版本信息)

可以配置其他头文件,例如 Cache-Control 您的语言(PHP,CGI,Java,htaccess)和webserver(Apache等)。


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.

解决方案

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>

I do not know if that would work. Not all HTTP headers can be configured directly in the HTML.

This works as an alternative to many HTTP headers, but see @EricLaw's comment below. This particular header is different.

Caveat

This answer is strictly about how to set headers. I do not know anything about allowing cross domain requests.

About HTTP Headers

Every request and response has headers. The browser sends this to the webserver

GET /index.htm HTTP/1.1

Then the headers

Host: www.example.com
User-Agent: (Browser/OS name and version information)
.. Additional headers indicating supported compression types and content types and other info

Then the server sends a response

Content-type: text/html
Content-length: (number of bytes in file (optional))
Date: (server clock)
Server: (Webserver name and version information)

Additional headers can be configured for example Cache-Control, it all depends on your language (PHP, CGI, Java, htaccess) and webserver (Apache, etc).

这篇关于如何使用Access-Control-Allow-Origin?它只是进入html头标记之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆