1

我正在尝试从 imdb 加载数据,但我在表 (GridPanel) 中没有结果。这是我的源代码:

...
<body>
<script type="text/javascript">
 Ext.onReady(function(){

var store1 = new Ext.data.JsonStore({
    root: 'root',
    idProperty: 'ID',
    remoteSort: true,
    fields: [
        'Title'
    ],
    // load using script tags for cross domain, if the data in on the same domain as
    // this page, an HttpProxy would be better
    proxy: new Ext.data.ScriptTagProxy({
        url: 'http://www.imdbapi.com/?t=True%20Grit'
    })
});
 // building grid panel
});
</script>
<div id="topic-grid"></div>
...

也许我应该更改 JsonStore 中的“root”参数?


更新

我尝试使用 HttpProxy,但仍然没有结果。我把我所有的身体内容都放了也许会更有帮助。

<script type="text/javascript">
Ext.onReady(function(){

var store1 = new Ext.data.JsonStore({

    reader: new Ext.data.JsonReader({
        fields: ['Title'],
        root: 'rows'
        }),

    // load using script tags for cross domain, if the data in on the same domain as
    // this page, an HttpProxy would be better
    proxy: new Ext.data.HttpProxy({
        url: 'http://www.imdbapi.com/?t=True%20Grit'
    }),
    autoLoad: true
  });

var grid1 = new Ext.grid.GridPanel({
    width:700,
    height:500,
    title:'ExtJS.com - Browse Forums',
    store: store1,
    trackMouseOver:false,
    disableSelection:true,
    loadMask: true,

    // grid columns
    columns:[{
        id: 'Title', 
        header: "Topic",
        dataIndex: 'Title',
        width: 420,
        sortable: true
    }]
});


// render it
grid1.render('topic-grid');

// trigger the data store load
store1.load({params:{start:0, limit:25}});
});
</script>
<div id="topic-grid"></div>
4

1 回答 1

3

使用 ScriptTagProxy 时,您无法直接从响应中获取 JSON。您只能获得可执行的 javascript,不幸的是,imdbapi 站点不返回可执行的 javascript。此外,您不能使用 HttpProxy 进行跨站点脚本 (XSS)。您只能连接到您自己本地域上的资源(例如,文件)。

一种可能性:

  1. 在您自己的域上设置一个服务器端文件,您的代理将连接到该文件。
  2. 使用与您的服务器端文件联系的 HttpProxy 而不是 ScriptTagProxy。

    proxy: new Ext.data.HttpProxy({
        url: '/path/to/my/server/file?t=True%20Grit'  // the leading slash in 
                                                      // this url will begin from
                                                      // your web server's root
                                                      // directory for your
                                                      // web-accessible files
    })
    
  3. 让您的服务器端文件代表客户端调用 imdb api,并将 imdb api 的结果作为 JSON 输出回客户端。

    myServerSideFile
    ================
    
    // harvest GET parameters, e.g., in your case, the query param 't' with value
    // True%20Grit
    
    // use the GET parameters to form a url with the GET params on the end, e.g.,
    // in your case, http://www.imdbapi.com/?t=True%20Grit
    
    // call the imdb api using this url
    
    // return the imdb api results as a JSON
    

有关在各种服务器端技术中执行上述建议的更多详细信息和示例,请参阅此内容。

于 2011-10-29T05:42:22.270 回答