gpt4 book ai didi

couchdb - angularjs跨域访问couchdb的方法

转载 作者:行者123 更新时间:2023-12-02 07:42:29 25 4
gpt4 key购买 nike

我已经设置了一个本地环境,我从一个 json 文件加载数据:

var Players = $resource('data/players.json');
var players = Players.query(function(){});

而且效果很好。

现在我想访问不同域 (http://playball.iriscouch.com/players) 上的 CouchDB,并且基于 google buzz 示例,我尝试了以下方法,但我无法使其工作:

var Players = $resource('http://playball.iriscouch.com/players',  
{alt: 'json', callback: 'JSON_CALLBACK'},
{get: {method: 'JSON'}
});

var players = Players.query(function(){});

我只是收到一个空数组...有什么想法吗?

非常感谢!

最佳答案

如果您可以控制服务器,请使用 CORS .

要更正您的示例:

var Players = $resource('http://playball.iriscouch.com/players',

// These params will be set to each request (?alt=json&callback=JSON_CALLBACK),
// where JSON_CALLBACK placeholder will be replaced with correct callback fn,
// generated by Angular.
{alt: 'json', callback: 'JSON_CALLBACK'},

// You need to configure query method to use JSONP
{query: {method: 'JSONP'}
});

var players = Players.query(function(){});

一定要检查服务器是否响应正确的数据(开发工具栏 - 网络面板)。

关于couchdb - angularjs跨域访问couchdb的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9609664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com