XMLHttpRequest无法加载http:// localhost:8080/exists/rest/db/.... Access-Control-Allow-Origin不允许使用null

Pau*_*aul 6 ajax rest jquery exist-db

我正在尝试使用对eXist DB REST API(1)的ajax调用来检索XML响应.

谷歌浏览器为我提供了以下控制台错误:

XMLHttpRequest cannot load XMLHttpRequest cannot load http://localhost:8080/exist/rest/db/movies?_query=%2Fmovies. Origin null is not allowed by Access-Control-Allow-Origin Origin null is not allowed by Access-Control-Allow-Origin
Run Code Online (Sandbox Code Playgroud)

虽然Firefox中的Firebug提供以下控制台错误:

GET http://localhost:8080/exist/rest/db/movies?_query=%2Fmovies 200 OK X 35ms jquery.min.js(line 18)
Run Code Online (Sandbox Code Playgroud)

这是代码的ajax部分:

$.ajax({
                    type: 'GET',
                    dataType: 'xml',
                    url: 'http://localhost:8080/exist/rest/db/movies',
                    data: {
                        _query: _query_value,
                        _key: _key_value,
                        _indent: _indent_value,
                        _encoding: _encoding_value,
                        _howmany: _howmany_value,
                        _start: _start_value,
                        _wrap: _wrap_value,
                        _source: _source_value,
                        _cache: _cache_value
                    },
                    success: function(resp){
                        alert("Resp OK!");
                        console.log(resp);
                    },
                    error: function(hqXHR, textStatus, errorThrown){
                        var x=0;
                        alert("fail0 " + hqXHR + " " + textStatus + " " + errorThrown);
                    }
                });
Run Code Online (Sandbox Code Playgroud)

我已经阅读了关于同一问题的一些相关问题,但我发现我尝试的任何解决方案都没有运气.

包含上面ajax代码段的index.html可以从我的本地机器访问,如下所示:

file:///.../index.html
Run Code Online (Sandbox Code Playgroud)

我尝试执行chrome如下:

chrome.exe --allow-file-access-from-files
Run Code Online (Sandbox Code Playgroud)

此外,我尝试将文件部署到在线主机,以便它位于

http://www.<somedomain>.com/index.html
Run Code Online (Sandbox Code Playgroud)

我最接近的解决方案是将from xml的dataType更改为jsonp.收到了响应,但由于预计它是xml,因此错误与解析相关,我认为,谷歌Chrome控制台返回以下语句:

Uncaught SyntaxError: Unexpected token <
Run Code Online (Sandbox Code Playgroud)

和Firebug控制台:

XML can't be the whole program
[Break On This Error]   
</exist:result>
movies...3302057 (line 528, col 15)
Run Code Online (Sandbox Code Playgroud)

回到我的问题,如何通过eXist DB正确检索xml文档?我究竟做错了什么?

期待您的帮助.我对网络开发很陌生,而且我的想法已经不多了.

提前致谢!

其他说明:

我正在使用的jquery.min.js位于http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

http://localhost:8080/exist/rest/db/movies?_query=%2Fmovies直接键入任何浏览器时,URI都有效.

参考:

(1) - http://www.exist-db.org/exist/devguide_rest.xml;jsessionid=e7096vtg4l7gmocouhe6zhle
Run Code Online (Sandbox Code Playgroud)

更新 (1)似乎我能够通过将eXist DB作为服务器运行来解决CORS问题,如下所示:bin/server.sh(在UNIX中)或bin\server.bat(在Windows中).

现在,我遇到了另一种问题.我使用jquery.js在Google Chrome控制台中收到以下错误:

GET http://localhost:8080/exist/rest/db/movies?_query=%2Fmovies  jquery.js:8240
jQuery.ajaxTransport.send jquery.js:8240
jQuery.extend.ajax jquery.js:7719
(anonymous function) index.html:43
jQuery.event.dispatch jquery.js:3332
jQuery.event.add.elemData.handle.eventHandle jquery.js:2941
Run Code Online (Sandbox Code Playgroud)

并使用jquery.min.js:

GET http://localhost:8080/exist/rest/db/movies?_query=%2Fmovies  jquery.min.js:18
f.support.ajax.f.ajaxTransport.send jquery.min.js:18
f.extend.ajax jquery.min.js:18
(anonymous function) index.html:43
f.event.handle jquery.min.js:17
f.event.add.i.handle.k jquery.min.js:16
Run Code Online (Sandbox Code Playgroud)

错误是什么意思?我的代码中哪一部分是错误的?

(2)显然,更新1是我遇到的一个不必要的问题.服务器模式无法使用我的机器,这就是上述错误出现的原因.

use*_*654 4

这是跨域请求,是不允许的。从 localhost 而不是 file:// 进行测试。此外,如果远程服务器打算与 JSONP 以外的任何数据类型跨域工作,则需要返回正确的CORS标头。JSONP 数据类型不适用于返回 XML。