使我的jQuery Ajax脚本使用CORS [英] Make my jQuery Ajax script use CORS

查看:294
本文介绍了使我的jQuery Ajax脚本使用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个应用程序,通过AJAX从外部网站读取数据。它工作正常,但我发现在另一个问题,如果我想打包为黑莓7,与Webworks或Phonegap,我可能需要使用 CORS

I built an application which reads data via AJAX from an external website. It works fine but I found out in another question that if I want to package it for BlackBerry 7, with Webworks or Phonegap, I may need to use something called CORS.

如何转换我的下面的脚本做同样的事情,除非使用'CORS'?

How can I convert my following script to do the same thing except using 'CORS'?

    <script type="text/javascript">
        $("#page_all").live('pagebeforecreate', function() {
        $.get('http://mysite.com/mobile/data/data_all.php',function(data){
            $('.content').empty();
            $(data).find('market').each(function(){
                var $market = $(this);
                var html = '<div class="data">';                      
                html += '<div data-role="collapsible" data-collapsed="true" data-theme="b"><h3>' + $market.attr('date') + '</h3>';
                html += '</div>';
                $('#result').append(html).trigger( "create" );
                $('#result .loading').remove();
            });                        
        });
        });
    </script>


推荐答案

从其他域名,并带来跨域的轮询。

you are probably hitting the domain (ontariosheep.org) from other domain name, and that brings the cross-domain into the poll.

CORS只是一种方法来解决这个问题,它必须是你的服务器主机 data_all.php 需要设置为在响应头中还具有属性:

CORS is just a way to solve this, and it has to be your server that hosts data_all.php that needs to be set to also have in the response header the property:

Access-Control-Allow-Origin: *

或者,您可以使用其他方法 JSONP

or, you can use the other common method called JSONP.

使用JSONP方法,你调用应该看起来像:

using JSONP method, you call should look like:

var url = "http://ontariosheep.org/mobile/data/data_all.php";
$.get(url + "?callback=?", function(data) {
    // your method body     
});

这篇关于使我的jQuery Ajax脚本使用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆