2012-03-12 6 views
6

JQueryを使用してURLから情報を取得し、非同期でページに表示しています。 URLは他のドメインから来ているので、JSONPを使ってデータを取得します。それはうまく動作します。AJAX + cross-domain + jsonpを使用してURLに到達できるかどうかをテストできますか?

しかし、リモートURLがダウンしていると(たまに起こる)、JQuery AJAXは「成功」または「エラー」機能を呼び出さないため、ページがハングアップします。

私はJQuery 1.7を使用しています。 "somePage" はアップしている場合、私はメッセージ "OK" を参照してください、

$.ajax({ 
     type : "GET", 
     url : "http://otherdomain.com/somePage.html", 
     data : params, 
     dataType : "jsonp", 
     jsonp : "jsonp", 

     success : function (response, textS, xhr) { 
      alert("ok"); 
     }, 
     error : function (xmlHttpRequest, textStatus, errorThrown) { 
      alert("not ok " + errorThrown); 
     } 
    }); 

私のコードは次のようになります。 「somePage」に到達できない場合は、何も表示されません。

「エラー」機能を呼び出すにはどうすればいいですか?さらに重要なことは、クロスドメインURLが到達可能かどうかを検出する方法ですか?

これは可能ですか? ** [クロスドメインのXMLHttpRequestsのための検出、サーバ/サイトをサポートしていますか?](http://stackoverflow.com:

おかげで、

+2

やや関連を追加します/ questions/9433949/detect-server-site-support-for-cross-domain-xmlhttprequests)** – hippietrail

答えて

10

timeout

$.ajax({ 
     type : "GET", 
     url : "http://otherdomain.com/somePage.html", 
     data : params, 
     timeout:3000, 
     dataType : "jsonp", 
     jsonp : "jsonp", 

     success : function (response, textS, xhr) { 
      alert("ok"); 
     }, 
     error : function (xmlHttpRequest, textStatus, errorThrown) { 
      alert("not ok " + errorThrown); 
      if(textStatus==='timeout') 
       alert("request timed out"); 
     } 
    }); 

DEMO

+0

ありがとう!私はとてもシンプルだったのでうれしいです。 – jmend

+1

jsonpとは何ですか: "jsonp"は? – suyash

関連する問題