`
sailei1
  • 浏览: 127016 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Sencha Touch Ajax CORS 跨域

阅读更多
CORS
全称:Cross-Origin Resource Sharing

在ST的 文档中是这么写的

A relatively new capability of modern browsers is called CORS, which stands for Cross-Origin Resource Sharing. This allows you to send requests to other domains without the usual security restrictions enforced by the browser. Sencha Touch 2 has support for CORS, though you'll probably need to do a little setup on your web server to enable it. If you're not familiar with what you need to do on your web server to enable CORS, a quick google search should give you plenty of answers.

Assuming your server is set up though, sending a CORS request is easy:
Ext.Ajax.request({
    url: 'http://www.somedomain.com/some/awesome/url.php',
    withCredentials: true,
    useDefaultXhrHeader: false
});

Form Uploads

The final thing we'll cover is uploading forms. This is also really easy:
Ext.Ajax.request({
    url: 'myUrl',
    form: 'myFormId',

    callback: function(options, success, response) {
        if (success) {
            Ext.Msg.alert('Success', 'We got your form submission');
        } else {
            Ext.Msg.alert('Fail', 'Hmm, that did not work');
        }
    }
});

This finds a <form> tag on the page with id="myFormId", grabs its data and puts it into the request params object just like at the start of this guide. Then it submits it to the url you specified and calls your callbacks like normal.

查了一下相关资料 确实是可以实现的


服务器端对于CORS的支持,主要就是通过设置Access-Control-Allow-Origin来进行的。如果浏览器检测到相应的设置,就可以允许Ajax进行跨域的访问。

Apache:Apache需要使用mod_headers模块来激活HTTP头的设置,它默认是激活的。你只需要在Apache配置文件的<Directory>, <Location>, <Files>或<VirtualHost>的配置里加入以下内容即可:

Header set Access-Control-Allow-Origin *

public void doOptions(Representation entity) {
    Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers"); 
    if (responseHeaders == null) { 
        responseHeaders = new Form(); 
        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); 
    } 
    responseHeaders.add("Access-Control-Allow-Origin", "*"); 
    responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS");
    responseHeaders.add("Access-Control-Allow-Headers", "Content-Type"); 
    responseHeaders.add("Access-Control-Allow-Credentials", "false"); 
    responseHeaders.add("Access-Control-Max-Age", "60"); 
} 	


PHP:只需要使用如下的代码设置即可。

<?php  
 header("Access-Control-Allow-Origin:*"); 


也可以这样 指定你的域名
 Access-Control-Allow-Origin: http://www.somedomain.com

浏览器支持情况


参考 http://blog.csdn.net/hfahe/article/details/7730944
  • 大小: 31.5 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics