【问题标题】:Nativescript API call return with HttpErrorResponseNativescript API 调用返回 HttpErrorResponse
【发布时间】:2019-06-10 17:43:07
【问题描述】:

尝试访问以下 API,但访问时不断收到错误消息。该 url 是正确的,并且在通过邮递员运行时可以完美运行。

let data = this.http.get<any>('http://chargepoints.dft.gov.uk/api/retrieve/registry/lat/53.790224/long/-1.563929/dist/3/format/json');

仅此调用会返回此错误

CONSOLE ERROR
file:///app/tns_modules/@angular/core/bundles/core.umd.js:15769:28: 
ERROR {
    "headers": {
    "normalizedNames": {},
    "lazyUpdate": null,
    "headers": {}
},
"status": 0,
"statusText": "Unknown Error",
"url":"http://chargepoints.dft.gov.uk/api/retrieve/registry/lat/53.790224/long/-1.563929/dist/3",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure response for http://chargepoints.dft.gov.uk/api/retrieve/registry/lat/53.790224/long/-1.563929/dist/3: 0 Unknown Error",
"error": {
"line": 1298,
"column": 38,
"sourceURL": "file:///app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js",
"originalStack": "ZoneAwareError@file:///app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:1298:38\nfile:///app/tns_modules/tns-core-modules/http/http-request/http-request.js:97:37\nUIApplicationMain@[native code]\n_start@file:///app/tns_modules/tns-core-modules/application/application.js:275:26\nrun@file:///app/tns_mo<…>

如果我只是将路径更改为不同的 api,它绝对可以正常工作。 谁能指出我所缺少的正确方向。

所以我已经更新了 AndroidManifest,现在运行良好,但是通过 iOs 我仍然遇到同样的问题。

【问题讨论】:

标签: nativescript nativescript-angular


【解决方案1】:

您需要在 HTTP 调用中传递内容类型

let httpOptions = { headers: new HttpHeaders(
                    {
                    'Content-Type': 'application/json'
                    })}

 this.http.get<any>(url, httpOptions).subscribe(payloadResponse => {
                        console.log('response received', payloadResponse);                            
                    }, error => {
                      // failed 
                    });

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 2019-03-22
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多