如何执行一个Ajax请求的CouchDB上(http://< USENAME> .couchone.com /)http、LT、Ajax、CouchDB

2023-09-10 16:15:56 作者:繁花落尽ヽ散落一夜空城

我想创建一个简单的AJAX(通过jQuery)请求到http:// yourusername .couchone.com /(alsmost在本地主机一样的,如果我已经安装的CouchDB)

I'm trying to create a simple AJAX (via jQuery) request to http://yourusername.couchone.com/ (alsmost the same as if I had installed couchdb on localhost)

如果我去的http:通过浏览器//**yourusername**.couchone.com/ 我会得到: {CouchDB的:欢迎,版本:1.0.1} 所以,它看起来像一个序列化的JSON

If I go to http://**yourusername**.couchone.com/ via Browser I'll get: {"couchdb":"Welcome","version":"1.0.1"} So, it looks like a serialized JSON.

所以我写了一个JS code:

So I wrote a JS Code:

$(function() {
        $.getJSON('http://www.********.couchone.com/', function(data) {
                console.log(data.couchdb);
                console.log(data.version);

            });
    });

不过,code不起作用。 Firebug的控制台显示GET请求没有响应(整条生产线是红色的)一切我能看到的是一个请求标题和响应头,但没有数据(如响应)

But the code doesn't work. FireBug's console shows that the GET request has no response (the whole line is in red) Everything I can see is a Request-Header and Response-Header, but NO DATA (as response)

请求标题:

Host :  www.*******.couchone.com
User-Agent :    Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 FirePHP/0.4
Accept :    application/json, text/javascript, */*
Accept-Language :   de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding :   gzip,deflate
Accept-Charset :    ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive :    115
Connection :    keep-alive
Origin :    null

响应头:

Server :    CouchDB/1.0.1 (Erlang OTP/R13B)
Date :  Sun, 26 Sep 2010 12:45:47 GMT
Content-Type :  application/json
Content-Length :  40
Cache-Control :  must-revalidate

想法?建议?

Ideas? Suggestions?

P.S。对不起,我的英语很差

P.S. Sorry for bad English

推荐答案

跨站点的安全模型$ P ​​$ pvents你做的JSON请求到不同的域。

Cross-site security model prevents you from doing JSON requests to a different domain.

您需要使用 JSONP 来能够实现这一目标。它不要求为<脚本> 包括,而不是一个XMLHTT prequest。 <脚本> 包括不具有相同的安全模型。

You need to use JSONP to be able to accomplish that. It does the request as a <script> include instead of an XMLHTTPRequest. <script> includes do not have the same security model.

我不知道,如果CouchDB的支持JSONP虽然。通常对于JSONP请求是这样的:

I don't know if couchdb supports JSONP though. Usually the request for JSONP looks like this:

http://someUrl/somePath?jsonp=mycallback

响应数据读取JSONP参数,返回有效的JavaScript在父页面的contenxt执行:

The response data reads that jsonp parameter and returns valid javascript to execute in the parent page's contenxt:

myCallback({ JSON:data, JSON:data });

您必须确保您信任的JSONP提供商,因为你基本上给他们的JavaScript执行访问你的页面。你的情况,你可能会做,因为这是你自己的CouchDB数据库。

You have to be sure you trust the JSONP provider, because you're essentially giving them javascript execution access to your page. In your case you probably do since it's your own couchdb database.

有没有其他的解决办法,标准$ .getJSON()不会,如果传递的网址是不是同一个域中的网页的工作。

There is no other solution, standard $.getJSON() will not work if the passed URL is not the same domain as your page.

P.S。我看着couchone.com,我没有看到任何这表明他们支持JSONP。你会需要你自己的服务器端脚本,这个脚本只是将请求转发给couchone并发送回响应批发(其中有隐藏你的真实的CouchDB提供URL的优势),或者找到一个不同的供应商,支持JSONP

P.S. I looked at couchone.com, and I don't see anything which suggests they support JSONP. You're going to need your own server-side wrapper script that simply forwards the request to couchone and sends back the response wholesale (which has the advantage of hiding your actual couchdb provider URL), or else to find a different provider which supports JSONP.

 
精彩推荐
图片推荐