投放本站广告请联系:
extjscn#126.com
ScriptTagProxy解决跨域json调用
相信很多朋友有时候会调用一些跨域的json,这时候用Ext.data.HttpProxy 就不灵了,这是由于安全方面的原因.Ext也为我们提供了另一个专门跨域调用的类,Ext.data.ScriptTagProxy,下面我们来看看这一个方法如何使用.
我们先来看一段Ext例子里面的代码:
.JS代码如下
// create the Data Store
var store = new Ext.data.Store({
// 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://extjs.com/forum/topics-browse-remote.php'
}),
// create reader that reads the Topic records
reader: new Ext.data.JsonReader({
root: 'topics',
totalProperty: 'totalCount',
id: 'threadid',
fields: [
'title', 'forumtitle', 'forumid', 'author',
{name: 'replycount', type: 'int'},
{name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
'lastposter', 'excerpt'
]
}),
// turn on remote sorting
remoteSort: true
});
store.setDefaultSort('lastpost', 'desc');
topics-browse-remote.php
<?php
//Create json
$json_data = "......";
// Grab the callback variable sent with every ScriptTagProxy request
$callback = $_REQUEST['callback'];
// Return the JSON string within the callback function, so ExtJS can interpret the response
print $callback.'('.$json_data.')';
?>
这样就完成了,但有时候这一个文件既要给HttpProxy调用,又要给ScriptTagProxy调用.同一个文件可以完成吗?当然可以.如以下代码:
<?php
//Create json
$json_data = "......";
// Grab the callback variable sent with every ScriptTagProxy request
$callback = $_REQUEST['callback'];
// Return the JSON string within the callback function, so ExtJS can interpret the response
if(!empty($callback))
$json_data = $callback.'('.$json_data.')';
print $json_data;
?>
- 关键字:
- 要发表评论,请先登录

