我有一个Angular应用程序进行跨源调用。所有的GET调用都很好用。DELETE调用,但是引发错误:
XMLHttpRequest cannot load http://my.url.com:8080/api/invoices/12768. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
在后端,我将JAX-RS与Glassfish 4.1和Java 8结合使用。我正在创建ResponseBuilder,如下所示:
ResponseBuilder responseBuilder = Response
.status(status)
.type(MediaType.APPLICATION_JSON)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "POST, GET, DELETE")
.header("Access-Control-Max-Age", "1209600")
.allow("OPTIONS");
在Angular中,我像这样进行服务调用:
$http.delete(requestUrl)
.success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (data, status, headers, config) {
deferred.reject(status);
});
事实是,这很好用:
$http.get(requestUrl)
.success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (data, status, headers, config) {
deferred.reject(status);
});
知道为什么GET可以工作,但不能删除吗?
编辑:
另一个要注意的是,我知道它不是服务器端,因为如果我通过本地计算机上的Advanced Rest Client运行delete调用,则DELETE可以正常工作。
编辑2:
请求头
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/{ipaddress} Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,es;q=0.6
Cookie: treeForm_tree-hi=treeForm:tree:applications
响应头
Server: GlassFish Server Open Source Edition 4.1
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8)
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, DELETE
Access-Control-Max-Age: 1209600
Allow: OPTIONS
Content-Type: application/json
Date: Fri, 07 Nov 2014 23:39:10 GMT
内容长度:0
这些是正在使用的Advanced Rest Client的标题
好的,显然,Angular从标头中剥离了内容类型。使用此作品:
$http.delete({url:request,headers:{'Content-Type':'application/json'}})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句