2010-06-02 22 views
0

我有http://mysite.com:8086/project/的Grails:JavaScript集成

ApiController Grails的服務器渲染(在我的情況的HTML代碼)一些文本http://mysite.com:8086/project/api/lastorders

我有另一臺服務器http://othersite.net/ - 這是用HTML頁面簡單的Apache服務器。

我想顯示從我的頁面上的服務器返回的文本。

我已經試過:

<script type="text/javascript"> 
new Ajax.Request("http://mysite.com:8086/project/api/lastorders", { 
    method: 'get', 
    onSuccess: function(response){ 
    alert(response.responseText); 
    }, 
    onFailure: function(){ 
    alert('ERROR'); 
    } 
}); 
    </script> 

,但反應是空的...

回答

1

如果你想從http://othersite.net/訪問http://mysite.com:8086/project/api/lastorders,不能做到這一點,由於跨域限制。如果你想解決這個問題,最常見的方法是設置http://othersite.net/來代表請求你感興趣的服務器獲取數據,然後返回該請求的結果。還有其他的解決方案,如使用iframe等。

欲瞭解更多信息,請參閱http://www.w3.org/TR/XMLHttpRequest/並搜索「同源」 - 這是限制你在這裏的政策。

0

當你在幕後使用Groovy,你讓這樣的代理:

class SomeController { 
    def someAction = { 
     render(new URL(params.url).text()) 
    } 
} 

,有你的JavaScript調用該控制器/動作

new Ajax.Request("${createLink(controller: "some", action: "someAction", params: [url: "http://xxx.yy"])}", { 
    method: 'get', 
    onSuccess: function(response){ 
    alert(response.responseText); 
    }, 
    onFailure: function(){ 
    alert('ERROR'); 
    } 
})