开发者

jQuery.get not receiving data

I have the following html + jquery:

<body>
    <head>
            <script type="text/j开发者_StackOverflow中文版avascript" src="jquery.js"></script>
            <script>
                $(window).load(function() {
                    $('#test').bind('click', function() {
                        $.get("http://localhost:3000/promotions.xml", function(data){
                            alert("Data Loaded: " + data);
                        });                        
                    });                 
                });
            </script>
    </head>

    <input id="test" type="button" value="Testando">
</body>

The problem is that the data parameter is empty, even though when I test the url on the browser I see a xml file with some data into it. I tried switching to HTML and json and it didn't work either. The server reports that everything is fine:

Started GET "/promotions.xml" for 127.0.0.1 at Thu Feb 10 16:02:33 -0200 2011
  Processing by PromotionsController#index as XML
  ←[1m←[35mPromotion Load (1.0ms)←[0m  SELECT "promotions".* FROM "promotions"
Completed 200 OK in 41ms (Views: 31.0ms | ActiveRecord: 1.0ms)

So I guess it is something in javascript/jQuery that I'm missing. I'm using jquery 1.4.4. What's wrong?


I'd guess that it is a result of the Same Origin Policy, which requires that AJAX requests be made to the same location as the page's origin.

If the page isn't hosted from localhost:3000, that would violate the policy.

If you're testing with Chrome, you should be able to disable the security policy by launching it from a terminal application with:

--disable-web-security
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜