Ext对ajax跨域问题的解决方案

跨域访问是ajax中比较头疼的事情。
Ext的Ext.data.ScriptTagProxy对象是用于解决这个问题的。
这个对象实际上做的事情就是使用动态script标签来处理跨域的请求问题。

[b]script标签的主要优点在于它并不受Web浏览器跨域安全限制的束缚,以及比
XMLHttpRequest具备更好的浏览器兼容性。[/b]

[b]ScriptTagProxy的使用方式比较简单,只用在构造函数中设置一个url。[/b]
示例代码:
[code] var ds = new Ext.data.Store({
proxy: new Ext.data.ScriptTagProxy({
url: 'http://extjs.com/forum/topics-remote.php'
}),
reader: new Ext.data.JsonReader({
root: 'topics',
totalProperty: 'totalCount',
id: 'post_id'
}, [
{name: 'title', mapping: 'topic_title'},
{name: 'topicId', mapping: 'topic_id'},
{name: 'author', mapping: 'author'},
{name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
{name: 'excerpt', mapping: 'post_text'}
])
});[/code]
[b]这个url返回的应该是javascript类型的内容,java代码示例:[/b]
[code]boolean scriptTag = false;
String cb = request.getParameter("callback");
if (cb != null) {
scriptTag = true;
response.setContentType("text/javascript");
} else {
response.setContentType("application/x-json");
}
Writer out = response.getWriter();
if (scriptTag) {
out.write(cb + "(");
}
out.print(dataBlock.toJsonString());
if (scriptTag) {
out.write(");");
}[/code]
[b]ScriptTagProxy会把这段javascript动态添加到页面中,如果javascript中的内容是json格式的,
可以用Ext.data.JsonReader进行解析。[/b]
在地址栏中输入
[url]http://extjs.com/forum/topics-remote.php?callback=strcall1001[/url]
就可以看到ext示例中的返回的javascript内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值