“针对(未知URL):0未知错误的Http故障响应”,

时间:2019-01-22 09:40:51

标签: angular typescript http httpclient nativescript

我直接与设备通信。

输入浏览器IP时

http://192.168.12.1/myconfig?pass=1&name=mywifi&passIS=87654321

在浏览器中显示以下消息:config is ok

现在,在我的项目中,我创建一个输入值的表单。在service.ts中,我创建了如下的POST方法:

public createConfiguration(NewConfiguration: Configuration) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/Json');
    let body = NewConfiguration.generateUrlencodedParameters();
    let url =  ('http://192.168.12.1/myconfig?')
    this.httpclient.post(url, body, {
       }).subscribe(d=>{
      console.log('response create newconfiguration', d)

    })
  }

此函数显示错误:

JS: ERROR {
JS:   "headers": {
JS:     "normalizedNames": {},
JS:     "lazyUpdate": null,
JS:     "headers": {}
JS:   },
JS:   "status": 0,
JS:   "statusText": "Unknown Error",
JS:   "url": null,
JS:   "ok": false,
JS:   "name": "HttpErrorResponse",
JS:   "message": "Http failure response for (unknown url): 0 Unknown Error",
JS:   "error": {
JS:     "originalStack": "Error: java.io.EOFException\n    at new c (file:///data/data/org.nativescript.myapp/files/app/vendor.js:1:1217697)\n    at file:///data/data/org.nativescript.myapp/files/app/vendor.js:1:1083743\n    at Object.onComplete (file:///data/data/org.nativescript.myapp/files/app/vendor.js:1:1084864)",
JS:     "zoneAwareStack": "Error: java.io.EOFException\n    at new c (file:///data/data/org.nativescript.myapp/files/app/vendor.js:1:1217697)\n    at file:///data/data/org.nativescript.myapp/files/app/vendor.js:1:1083743\n    at Object.onComplete (file:///data/data/org.nativescript.myapp/files/app/vendor.js:1:1084864)"
JS:   }
JS: }

消息config is ok在正文中。

请,您是否知道如何解决此错误?

1 个答案:

答案 0 :(得分:0)

按如下所示修改您的代码:

// in service.ts file

public createConfiguration(NewConfiguration: Configuration) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/Json');
    let body = NewConfiguration.generateUrlencodedParameters();
    let url =  ('http://192.168.12.1/myconfig?')
    this.httpclient.post(url, body, {});  // <-- you need to subscribe in component.ts file
  }
  
  
  //in compoenent.ts file  
  
  public callerMethod(){  //<- method which calls createConfiguration() service
  
  // srvcObj -> your service object name , paylaod -> input you are sending to servoce method
  this.srvcObj.createConfiguration(payload).subscribe((result:any)=>{
  
           console.log('response create newconfiguration', result);
  },
  (error)=>{
     console.log('got error',error);
  }
  );
  
  }

相关问题