2

我正在尝试使用 extjs 存储与返回 Json 的 Jersey REST Java 应用程序(在 tomcat 上运行)对话。我正在尝试使用 Json 打印到网格组件。这是我的商店代码。

Ext.define('WSC.store.Users', {
extend: 'Ext.data.Store',
fields: ['period','tot_units', 'tot_selling_price'],
model: 'WSC.model.User',
proxy: {
    type: 'rest',
    url : 'http://localhost:8080/mondrianCube/services/query/querygoeshere/json',
    reader: {
        type: 'json',
        root: 'table'
    }
},
autoLoad: true

});

商店无法读取获得的 json。大多数建议是将响应标头(Access-Control-Allow-Origin)添加到 webapp(在 tomcat 上运行)。所以我添加了如下的响应头。

@Path("/query/{qryParam}/json")
@GET
@Produces(MediaType.APPLICATION_JSON)
public static Response jsonResult(@PathParam("qryParam") String qryParam) throws JSONException 
{
    executeQuery("select {[Measures].members} on columns, {Time.[2010], Time.[2010]} on rows from sales" );
    String json = (new JSONObject(((new ResultSetConvert(result)).toJson()))).toString();
    return Response.ok(json).header("Access-Control-Allow-Origin","*").build();     
}

即使那样我也得到与下面相同的错误

http://i.stack.imgur.com/X2cKd.png

我在这里想念什么?

PS 如果 url 指向同域中的文件,则 store 能够读取文件中的 json。

4

1 回答 1

0

我相信你会想要为此使用 JSONP,例如,

            Ext.get('search-form').on('submit', function(ev) {
                ev.preventDefault();
                Ext.ux.JSONP.request('http://api.flickr.com/services/feeds/photos_public.gne', {
                    callbackKey: 'jsoncallback',
                    params: {
                        format: 'json',
                        tags: Ext.fly('search-value').dom.value,
                        tagmode: 'all',
                        lang: 'en-us'                            
                    },
                    callback: updateResults
                });
                return false;
            });

维基百科定义的 jsonp

JSONP 或“带填充的 JSON”是对基本 JSON 数据格式的补充。它提供了一种从不同域中的服务器请求数据的方法,由于相同的来源策略,典型的 Web 浏览器禁止这样做。

同源政策定义

在计算中,同源策略是许多浏览器端编程语言(例如 JavaScript)的重要安全概念。该策略允许在源自同一站点的页面上运行的脚本无特定限制地访问彼此的方法和属性,但禁止跨不同站点的页面访问大多数方法和属性。 [

于 2012-08-24T12:19:55.227 回答