2016-08-12 88 views
0

我正在使用來自@ angular/http的Http發送GET請求,但服務器未收到請求。生成的網址是正確的,因爲當我在瀏覽器中記錄它們並打開它們(我嘗試了所有的Chrome,Firefox和Safari)時,服務器確實會收到這些請求。離子2:http獲取請求不起作用(代理已添加)

這就是我正在做的:

let logButtonUrl = this.urlGenerator.generateTiramisuUrlTemp(this.servletPath, 
    argMap); 

console.log("logButtonUrl:"+logButtonUrl); 
return this.http.get(logButtonUrl).map(this.writeSuccess); 

功能writeSuccess:

private writeSuccess(res: Response) { 
    let body = res.json(); 
    let rows_affected = body.data[0].rowsAffected; 
    if (rows_affected == "1") { 
     return true; 
    } else { 
     return false; 
    } 

}

我得到了在瀏覽器控制檯沒有錯誤信息,所以它可能不是因爲此處討論的CORS問題: http://blog.ionic.io/handling-cors-issues-in-ionic/

我也嘗試使用代理。我在ionic.config.json中添加了這個:

{ 
"path": "/backendTemp", 
proxyUrl": "http://128.237.217.70:8080" /*the ip address of the target server*/ 
} 

並用「/ backendTemp」替換我生成的URL中的IP地址。還是行不通。

對此有何建議/想法?非常感謝!

回答

0

使用$ HTTP(https://docs.angularjs.org/api/ng/service/ $ HTTP):

.controller('RequestCtrl', function ($http) { 

    $http({ 
     method: 'GET', 
     url: 'http://128.237.217.70:8080/backendTemp' 
    }).then(function successCallback(response) { 
     // this callback will be called asynchronously 
     // when the response is available 
     }, function errorCallback(response) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
     }); 
+0

感謝您的答覆!這是針對ionic2還是ionic1? –

+0

@TeddyDing這是爲了任何,因爲它不是離子特定的。 $ http是一個Angular函數。 –

+0

謝謝!後來我發現我只需處理http.get返回的承諾。愚蠢的錯誤.... –