【问题标题】:EXCEPTION: Error during evaluation of "click" occurs only after the 1st click例外:评估“点击”期间的错误仅在第一次点击后发生
【发布时间】:2016-04-18 07:01:15
【问题描述】:

我正在做一个 ionic2 http post 服务。

使用“(点击)”功能,我的 http 帖子将被调用,一切都在第一次运行。当我第二次按下按钮时,奇怪的事情发生了。

弹出这个错误:

所以我试着玩弄它。我在调用 click 函数的方法中添加了一个 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());
}

我猜这与订阅服务有关。如果是这种情况,我如何关闭订阅以再次允许相同的请求?

【问题讨论】:

  • 我在您的问题中看不到任何(click)="xxx",也看不到有关单击时执行的功能的任何其他信息。
  • @GünterZöchbauer 您好,我已编辑!谢谢!

标签: javascript ionic-framework angular ionic2


【解决方案1】:

你的代码有点奇怪,特别是在这一行:

this.httpService.loginService("VariablesToThrowOver").subscribe(
  data => {this.httpService = data.results; console.log(data);}, // <------

实际上,您尝试将接收到的数据设置为您用来执行登录处理的服务变量本身。我很确定对象data.results 中没有loginService 方法。所以当你第二次点击的时候,就会报错...

我会选择组件的另一个(且不同的)属性来存储这些数据并将其显示到视图中。像这样的:

this.httpService.loginService("VariablesToThrowOver").subscribe(
  data => {
    this.results = data.results; // <------
    console.log(data);
  },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2017-07-03
    • 2013-04-20
    相关资源
    最近更新 更多