2016-08-12 48 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返回的承诺。愚蠢的错误.... –