javascript - 异常 : Error during evaluation of "click" occurs only after the 1st click

标签 javascript ionic-framework angular ionic2

我正在做 ionic2 http post 服务。

使用“(点击)”功能,我的http post将被调用,并且第一次一切正常。当我第二次按下按钮时,奇怪的事情发生了。

弹出此错误:

Error Message

所以我尝试着尝试一下。我在我的方法中添加了一个 console.log,其中调用了单击函数。

第三次按下按钮等时,没有错误消息,我的http帖子也不起作用。只有我的方法内的 console.log 在单击时打印。

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/

相关文章:

java - 第二个 Controller 方法调用时出现 401 错误

javascript - setMinutes 未得到预期结果

javascript - 将 d3 子弹图加载到跨度中

ionic-framework - 在 Ionic 2 中实现 Google Analytics 时出错

android - 推送通知仅适用于国外第三方推送提供商服务?

javascript - 变更检测和 Material 2

angular - 如何在 Angular 5 中的 Angular Material Snackbar 中添加图标

javascript - 如何在悬停时在 div 中平滑过渡? [Tumblr 更新选项卡]

javascript - 带有转换器的 Firebase 函数在 Set() 上删除文档引用

javascript - 如何使用参数插入多行?