通过ajax调用CouchDB时获得空响应

[英]Getting an empty response when calling CouchDB over ajax


I'm new to CouchDB, so please bear with me.

我是CouchDB的新手,所以请耐心等待。

I have an instance of CouchDB running on a VM. I can access it just fine through the browser via futon or directly at:

我有一个在VM上运行的CouchDB实例。我可以通过浏览器通过蒲团或直接访问它:

http://192.168.62.128:5984/articles/hot_dog

Calling that URL in a browser returns the proper JSON. But, when I try to call that exact same URL via ajax, I get nothing:

在浏览器中调用该URL会返回正确的JSON。但是,当我尝试通过ajax调用完全相同的URL时,我什么也得不到:

var ajaxUrl = 'http://192.168.62.128:5984/articles/hot_dog';
$.getJSON(ajaxUrl, null, function(data) { alert(data); });

Looking at the response header with Firebug shows me that the HTTP response was 200 and the content-length is the right size. Even the Etag matches with what is in CouchDB. But the response itself is empty!

查看Firebug的响应标头,向我显示HTTP响应为200,内容长度是正确的大小。甚至Etag也与CouchDB中的内容相匹配。但回应本身是空的!

The URL is absolutely right; I've triple checked, and copy/pasted it directly (and besides it wouldn't give a 200 response if it weren't). I'm using jQuery 1.4.2, and CouchDB 0.8

网址绝对正确;我已经三重检查,并直接复制/粘贴它(除此之外它不会给出200响应)。我正在使用jQuery 1.4.2和CouchDB 0.8

What's going on?

这是怎么回事?

4 个解决方案

#1


2  

Sounds very strongly like you're trying to make a cross-domain AJAX request, which the browser will reject. To get around this, you can use JSONP like the answer above, but this will restrict you to GET requests; you won't be able to add, modify, or delete records.

听起来非常强烈,就像你试图制作跨域AJAX请求一样,浏览器会拒绝这一请求。为了解决这个问题,您可以像上面的答案一样使用JSONP,但这会限制您使用GET请求;您将无法添加,修改或删除记录。

If you are trying to do cross-domain AJAX calls with CouchDB, I recommend checking out this library:

如果您尝试使用CouchDB进行跨域AJAX调用,我建议您查看此库:

http://github.com/benvinegar/couchdb-xd

http://github.com/benvinegar/couchdb-xd

#2


5  

Try appending callback=? to the url like this. This will trigger jQuery to issue a jsonp request.

尝试追加回调=?像这样的网址。这将触发jQuery发出jsonp请求。

var ajaxUrl = 'http://192.168.62.128:5984/articles/hot_dog?callback=?';

If this doesn't fix it additionally you should append a sample output of the json this url gives in the browser.

如果这不能另外修复它,你应该在浏览器中附加这个url给出的json的示例输出。

#3


5  

As you try to get the data from an other web server as your html file comes you need to do a JSONP query. First of all in the latest version of CouchDB you need to enable JSONP querys in the configuration (.ini) file. (It is disable by default since CouchDB 1.0) In the section [httpd] you need to add an

当您尝试从其他Web服务器获取数据时,您需要执行JSONP查询。首先,在最新版本的CouchDB中,您需要在配置(.ini)文件中启用JSONP查询。 (默认情况下禁用,因为CouchDB 1.0)在[httpd]部分,你需要添加一个

allow_jsonp = true

allow_jsonp = true

After this is done you can produce JSONP queries on your CouchDB.

完成此操作后,您可以在CouchDB上生成JSONP查询。

In jQuery then you have to add ?callback=? to the URL to trigger a JSONP query.

在jQuery中你必须添加?callback =?到URL以触发JSONP查询。

Happy Cross-Origin Resource Sharing everyone.

快乐的跨源资源分享每个人。

#4


-2  

firstly this is a json question and u have tagged jquery...

首先这是一个json问题,你已经标记了jquery ......

if you need jquery then ... look up load() function.

如果你需要jquery那么...查找load()函数。

$('#selector').load(ajaxURL); alert($('#selector').html())

$( '#选择')负载(ajaxURL);警报($( '#选择')。HTML())


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2010/03/23/449b89cddf2b0a4a5d5eec05129709af.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号