2013-03-12 8 views
16

私はajaxでクロスドメインの例を見ましたが、動作しません。クロスドメインjqueryを取得

私はクロームにしようとすると、次のエラーが指定されている
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

    <title></title> 
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
    </head> 
    <body> 

     <script type="text/javascript" > 
    $(document).ready(function() { 
      var url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AssasNet&include_rts=1"; 

      $.get(url, function (data) { 
     console.log(data) 
     alert(data); 
     }); 
     }); 

     </script> 

    </body> 
</html> 

XMLHttpRequest cannot load http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AssasNet&include_rts=1. Origin null is not allowed by Access-Control-Allow-Origin. 
+3

ここからの例はありますか?それは間違っています –

+2

あなたはここではできませんクロスドメインAJAXそのように使用するために特別に外部サーバーを設定する必要があります。 [代わりにJSON-Pを使用する](http://api.jquery.com/jquery.getJSON/) – Blazemonger

+1

また、ステートメントのみがあります。実際の質問ではありません。ちょうど "バグはどこにあるのですか"を提案してください –

答えて

38

それはAJAX呼び出しを行いますので、あなたは、クロス原点ので、Same Origin Policyによってブロックされる、$.getを使用することはできませんアクセスしようとしているTwitter APIはCross-Origin Resource Sharingをサポートしていません(そうであれば、試したものである起点nullまたはhttp://jsbin.comは許可されません)。 |

$.ajax({ 
    url: url, 
    dataType: "jsonp", 
    success: function (data) { 
    console.log(data) 
    alert(data); 
    } 
}); 

Live Example

APIはこれだけ$.ajax指定JSONP作品に$.getを変更する、しかし、(真のAJAX呼び出しではありません)サポートJSONPを行いますSource

関連する問題