我正在做 ionic2 http post 服务。
使用“(点击)”功能,我的http post将被调用,并且第一次一切正常。当我第二次按下按钮时,奇怪的事情发生了。
弹出此错误:
所以我尝试着尝试一下。我在我的方法中添加了一个 console.log,其中调用了单击函数。
在第三次按下按钮等时,没有错误消息,我的http帖子也不起作用。只有我的方法内的 console.log 在单击时打印。
这是我的 html:
<button class="button button-block" (click)="loginSuccess()" style="width:70%;">
<strong>Log In</strong>
</button>
这是我的js:
loginSuccess() {
console.log("Invoked Meod!");
this.httpService.loginService("VariablesToThrowOver").subscribe(
data => {this.httpService = data.results; console.log(data);},
err => console.log(err),
() => console.log('Invoked Login Service Complete'));
}
这是我的服务 js:
loginService(httpService) {
console.log("Service Function Variable: "+httpService);
var body = 'username=' + "admin" + '&password=' + "admin" +'';
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
var url = 'http://localhost/webservice/userrWS.asmx/login';
return this.http.post(url,body, {headers: headers}).map(res => res.json());
}
我猜这与订阅服务有关。如果是这种情况,我如何关闭订阅以再次允许相同的请求?
最佳答案
你的代码有点奇怪,特别是这一行:
this.httpService.loginService("VariablesToThrowOver").subscribe(
data => {this.httpService = data.results; console.log(data);}, // <------
事实上,您尝试将接收到的数据设置到用于执行登录处理的服务变量本身中。我非常确定对象 data.results
中没有 loginService
方法。所以当你第二次点击的时候,就会出现错误...
我会选择组件的另一个(且不同的)属性来存储此数据并将其显示到 View 中。像这样的事情:
this.httpService.loginService("VariablesToThrowOver").subscribe(
data => {
this.results = data.results; // <------
console.log(data);
},
关于javascript - 异常 : Error during evaluation of "click" occurs only after the 1st click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687320/