使用 jsonp 响应将数据添加到 couchdb

发布于 2024-09-10 23:13:35 字数 91 浏览 3 评论 0原文

有没有办法将数据添加到在另一个域上运行的 couchdb 并返回响应,无论操作是否成功?我知道 couchdb 支持 jsonp 回调,但是我可以用这种方法添加数据吗?

Is there a way add data to a couchdb that runs on another domain and get back an response whether the operation was successfully or not? I know couchdb supports jsonp callback but can I add data with this approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-09-17 23:13:35

不,您目前无法执行此操作。 CouchDB 的 REST API 需要 POST 或 PUT 请求才能插入数据,但 JSONP 仅支持 GET 请求。因此,您可以跨域从 CouchDB 检索数据,但更新/插入/删除不起作用。

No, you cannot currently do this. CouchDB's REST API requires a POST or PUT request in order to insert data, but JSONP only supports GET requests. So you can retrieve data from CouchDB across domains, but updates/inserts/deletes won't work.

又爬满兰若 2024-09-17 23:13:35

您可以使用客户端 JavaScript 制作一个表单来执行 POST,将输出定向到 iframe,并使用跨窗口 iframe 消息传递来获取结果。

当然,有人已经制作了一个很好的 javascript 库来做到这一点。在这里获取代码:
https://github.com/benvinegar/couchdb-xd

按照说明将其作为couchdb 服务器上的附加数据库。然后,在任何站点上,包括一个不在“your-couch-server”域中的站点,您可以执行以下操作(只需在 javascript 控制台中尝试):

jQuery.getScript(
    "http://YOUR-COUCH-SERVER/couchdb-xd/_design/couchdb-xd/couchdb.js", 
    function() {
        Couch.init(
            function() { 
                var s = new Couch.Server('http://YOUR-COUCH-SERVER/'); 
                var d = new Couch.Database(s,'YOURDB'); 
                d.put(
                    "stackoverflow-test 1", 
                    { foo: 111, bar: 222 },
                    function(resp) {
                        console.log(resp);          
                    } 
                );
            }
        )
    }
);

上面假设您已经在页面上加载了 jquery。如果没有,您需要添加它,但您当前正在与其他页面交互。

该库仅适用于支持 window.postMessage() 的现代浏览器,尽管一个小补丁最终可能允许旧浏览器通过 src/hash 通信使用它。

You can use client-side javascript to make a form to do the POST, direct the output to an iframe, and use cross-window iframe messaging to get the result.

Of course, someone has already made a nice javascript library to do this. Get the code here:
https://github.com/benvinegar/couchdb-xd

Follow the instructions to push it as an additional database on your couchdb server. Then, on any site, include one not in the 'your-couch-server' domain, you can do the following (just try it in the javascript console):

jQuery.getScript(
    "http://YOUR-COUCH-SERVER/couchdb-xd/_design/couchdb-xd/couchdb.js", 
    function() {
        Couch.init(
            function() { 
                var s = new Couch.Server('http://YOUR-COUCH-SERVER/'); 
                var d = new Couch.Database(s,'YOURDB'); 
                d.put(
                    "stackoverflow-test 1", 
                    { foo: 111, bar: 222 },
                    function(resp) {
                        console.log(resp);          
                    } 
                );
            }
        )
    }
);

The above presumes you have jquery is already loaded on the page. If not, you'll need to add it however you're currently interacting with the other page.

The library only works on modern browsers with window.postMessage() support, though a small patch may eventually allow older browsers to use it via src/hash communication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文