Json RPC jquery ajax调用

时间:2012-03-24 07:52:38

标签: php jquery json-rpc

我正在尝试在加载页面时发送json RPC ajax请求。这是我的javascript代码:

$(document).ready(function() {
        var data="orderID=<?=$_POST['orderID'];?>&&lang=eng";
        $.ajax({
            url: "ajax/get_json_variables.php", 
            type: "POST",       
            data: data,     
            cache: false,
            success: function (html) {
                var splited=html.split("|");
                if(splited[0]=="0")
                {
                    alert(splited[1]);
                }
                else
                {
                    $.ajax({
                        url: "https://91.199.226.106/ssljson.php",  
                        type: "POST",       
                        data: splited[1],       
                        cache: false,
                        dataType:"json",
                        success: function (html) {
                            alert(html);
                        }
                    });
                }
            }   
        });
    });

使用第一个ajax请求,我得到第二个请求的json字符串。 Google Chrome给我这个错误:

XMLHttpRequest cannot load https://91.199.226.106/ssljson.php. Origin http://www.nver.am is not allowed by Access-Control-Allow-Origin.

我没有得到什么问题?谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

问题归因于Same Domain Policy,解决这个问题可能很棘手。如果你没有使用jQuery,你可以通过几种方式来解决这个问题并且仍然可以实现所需的功能,那就是使用iframe和跨域消息助手(例如EasyXDM),或动态注入标记来获取你的JSON以称为JSONP的特殊格式。

值得庆幸的是,您正在使用jQuery,因此您可以使用http://api.jquery.com/jQuery.getJSON/和JSONP来解决同源策略。

答案 1 :(得分:0)

我认为这可能是与“同源政策”相关的问题。您不能像执行不同的域或使用不同的协议执行不同的ajax调用。

这里有一些参考: http://en.wikipedia.org/wiki/Same_origin_policy http://stackoverflow.com/questions/1105934/ajax-http-https-problem http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing