2011-09-18 31 views
0

我想連接到使用jquery氛圍插件的彗星服務器。問題是服務器位於不同的域中,並且「自v 0.7.2開始,Atmosphere JQuery PubSub不支持跨域請求,我們不能使用此方法用於JSONP」。有什麼辦法可以繞過這個嗎?我願意接受任何解決方案!jquery大氣跨域限制

這是在給定的示例頁面中使用的遠程域的JavaScript代碼:

$(document).ready(function() { 

    function callback(response) { 
     // Websocket events. 
      $.atmosphere.log('info', ["response.state: " + response.state]); 
     $.atmosphere.log('info', ["response.transport: " + response.transport]); 

     detectedTransport = response.transport; 
     $('ul').append($('<li></li>').text("Resp Status : "+response.status+" Transport : "+response.transport)); 
     if (response.transport != 'polling' && response.state != 'connected' && response.state != 'closed') { 
     $.atmosphere.log('info', ["response.responseBody: " + response.responseBody]); 
     if (response.status == 200) { 
      var data = response.responseBody; 
      if (data.length > 0) { 
       dataArray = eval("("+data+")"); 

       $.each(dataArray, function(index, value){ 
       var output = ''; 
       var dipData = value; 
       for (property in dipData) { 
        output += property + ': ' + dipData[property]+'; '; 
       } 

       $('ul').append($('<li></li>').text(dipData.__publicationName+":"+output)); 
       }); 
      } 
     } 
     else{ 

     } 
     } 
    } 

     // As of v 0.7.2, Atmosphere JQuery PubSub does not support 
     // cross domain requests. We cannot use this method for JSONP 
     $.atmosphere.subscribe(document.location.toString() + 'webcast', callback); 
    }); 

感謝

回答

1

最簡單的方法可能是站起來的Node.js代理上您的域名經紀人您的請求到外部域。

+0

謝謝,我從來沒有碰過node.js。你能否給我一個例子說明它將如何實施?謝謝! – Alex

+0

+1表示node.js。 Nginx作爲一個簡單的代理服務器也非常簡單。 – tjameson

+0

我在服務器上使用nginx,也許它會是最好的方法;你能給我一個它如何工作的例子嗎?謝謝 – Alex

0

最簡單的解決方案是:在沒有IE瀏覽器和XDR或跨域請求的情況下使用CORS。 您只需修改HTTP標頭並使用Allow_Access即可。