Rafael Sedrakyan
Rafael Sedrakyan

Reputation: 2629

Json RPC jquery ajax call

I am trying to send a json RPC ajax request when the page is loaded. Here is my javascript code:

$(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);
                        }
                    });
                }
            }   
        });
    });

With the first ajax request I get json string for the second request. Google chrome gives me this error:

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

I don`t get what is the problem? Thanks for help.

Upvotes: 1

Views: 4249

Answers (2)

Alex Parker
Alex Parker

Reputation: 527

The issue is due to the Same Domain Policy, and it can be tricky to work around. If you were not using jQuery, a couple of ways you can get around this and still achieve the desired functionality would be to use iframes and a cross-domain messaging helper such as EasyXDM, or dynamically injected tags to fetch your JSON in a special format known as JSONP.

Thankfully you are using jQuery, so you can use http://api.jquery.com/jQuery.getJSON/ and JSONP to work around the Same Origin Policy.

Upvotes: 2

ab_dev86
ab_dev86

Reputation: 1982

I think this can be a problem related to "Same origin policy". You can't execute different ajax call to different domains or with different protocols as you are doing.

Here some references: 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

Upvotes: 0

Related Questions