显然,我完全误解了它的语义.我想到了这样的事情:
好吧,我错了.它根本不起作用.所以,我已经阅读了跨源资源共享,并尝试在w3c推荐中阅读跨源资源共享
有一件事是肯定的 - 我仍然不明白我应该如何使用这个标题.
我完全控制了站点A和站点B.如何启用从站点A下载的javascript代码以使用此标头访问站点B上的资源?
PS
我不想使用JSONP.
我想从jQuery向AJAX POST请求添加自定义标头.
我试过这个:
$.ajax({
type: 'POST',
url: url,
headers: {
"My-First-Header":"first value",
"My-Second-Header":"second value"
}
//OR
//beforeSend: function(xhr) {
// xhr.setRequestHeader("My-First-Header", "first value");
// xhr.setRequestHeader("My-Second-Header", "second value");
//}
}).done(function(data) {
alert(data);
});
Run Code Online (Sandbox Code Playgroud)
当我发送此请求并使用FireBug观看时,我看到此标题:
选项xxxx/yyyy HTTP/1.1
主机:127.0.0.1:6666
User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64; rv:11.0)Gecko/20100101 Firefox/11.0
接受:text/html,application/xhtml + xml, application/xml; q = 0.9,/ ; q = 0.8
Accept-Language:fr,fr-fr; q = 0.8,en-us; q = 0.5,en; q = 0.3
Accept-Encoding:gzip,deflate
Connection:keep -alive
Origin:null
Access-Control-Request-Method:POST
Access-Control-Request-Headers:my-first-header,my-second-header
Pragma:no-cache
Cache-Control:no-cache
为什么我的自定义标题会转到Access-Control-Request-Headers:
Access-Control-Request-Headers:my-first-header,my-second-header
我期待像这样的标头值:
My-First-Header:第一个值
My-Second-Header:第二个值
可能吗?
我在客户端使用AngularJS $ http访问服务器端ASP.NET Web API应用程序的端点.由于客户端作为服务器托管在不同的域上,因此我需要CORS.它适用于$ http.post(url,data).但是只要我通过$ http.get(url)对用户进行身份验证并发出请求,我就会收到消息
The 'Access-Control-Allow-Origin' header contains multiple values 'http://127.0.0.1:9000, http://127.0.0.1:9000', but only one is allowed. Origin 'http://127.0.0.1:9000' is therefore not allowed access.
Fiddler告诉我,在成功的选项请求之后,get请求中确实有两个头条目.我做错什么地方在哪里?
更新
当我使用jQuery $ .get而不是$ http.get时,会出现相同的错误消息.所以AngularJS似乎没有问题.但哪里错了?
我有一个带PHP的NGINX服务器(我们假设主机名为http://myserver.com).我有一个PHP脚本,我通过XHR从我的localhost上的网页访问.我将它用作类似于freegeoip.net的GeoIP服务器.
我正在尝试将XHR锁定到特定域.
这是我的配置设置:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param GEOIP_COUNTRY_CODE $geoip2_data_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip2_data_country_name;
fastcgi_param GEOIP_COUNTRY_GEONAME_ID $geoip2_data_country_geoname_id;
fastcgi_param GEOIP_CITY_NAME $geoip2_data_city_name;
fastcgi_param GEOIP_CITY_GEONAME_ID $geoip2_data_city_geoname_id;
fastcgi_param GEOIP_CONTINENT_CODE $geoip2_data_city_continent_code;
fastcgi_param GEOIP_CONTINENT_GEONAME_ID $geoip2_data_city_continent_geoname_id;
fastcgi_param GEOIP_LATITUDE $geoip2_data_city_location_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip2_data_city_location_longitude;
fastcgi_param GEOIP_TIME_ZONE $geoip2_data_city_location_timezone;
fastcgi_param GEOIP_ISP $geoip2_data_city_traits_isp;
fastcgi_param GEOIP_IP_ADDRESS $geoip2_data_city_traits_ip_address;
set $cors "";
if ($http_origin ~* 'https?://(www\.domain1\.com|www\.domain2\.com)')
{
set $cors "true";
}
if ($cors = 'true')
{
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true'; …Run Code Online (Sandbox Code Playgroud) 现在我使用laravel 5.2构建web应用程序作为webserivce和angular2作为首页我使用来自http://cdn.pannellum.org的 VR库 我的问题当我从mywebsite.com/public/Images/photo7我的数据库中调用Iamge的链接时.png回复给我这条消息
'access-Control-Allow-Origin'标头出现在请求的资源上.原产地" 的http://本地主机:5565 "因此不允许访问.
但我在开始项目从laravel和angular传输数据之前处理了这个问题但是当我从直接路径调用我的图像时返回给我这条消息
cors ×3
ajax ×2
javascript ×2
php ×2
angular ×1
cross-domain ×1
http-headers ×1
jquery ×1
laravel ×1
nginx ×1
post ×1