我正在使用Cors请求在我的客户端和位于两个不同域的服务器之间进行通信。
我以这种方式使用SSL来配置我的apache http服务器:
//with AJAX withCredentials=true (cookies sent, SSL allowed...)
SetEnvIfNoCase ORIGIN (.*) ORIGIN=$1
Header always set Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, PATCH, DELETE"
Header always set Access-Control-Allow-Origin "%{ORIGIN}e"
Header always set Access-Control-Allow-Credentials "true"
Header always set Access-Control-Allow-Headers "X-Accept-Charset,X-Accept,Content-Type"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]
我的ajax请求是这样的:
$.ajax({ url: URL,
type: 'PUT',
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: userPreferences,
success: function() { }
});
$.ajax({ url: URL,
type: 'GET',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function() { }
});
这个配置的结果是,我的获取请求正在工作,但不是我的put请求,我在谷歌Chrome控制台中得到以下错误(用firefox itried)
XMLHttpRequest cannot load https://URL. Method PUT is not allowed by Access-Control-Allow-Methods.
我该如何解决这个问题?
答案 0 :(得分:0)
我在apache config
上错误地输入了这一行Header always set Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, PATCH, DELETE"
我输入没有S的方法,这就是我收到此错误的原因:
XMLHttpRequest cannot load https://URL. Method PUT is not allowed by Access-Control-Allow-Methods.
无论如何,我上面写的所有配置都是经过测试/运行的解决方案,可以使用SSL来实现。