Cordova(IONIC)http帖子即使包含标题详细信息也无法正常工作

问题描述 投票:0回答:1

我正在使用Ionic 4创建一个小型移动应用。后端是C#WebAPI。我需要向服务器提交一些数据。而且它不起作用。

代码:

NewsDetails = {
    "ServerID": "1",
    "Title": "Test",
    "Source": "Self",
    "Body": "Hello world"
  };
  const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
        })
      };

    this.http.post("https://sitename.com/DataServer/News", this.NewsDetails, httpOptions)
      .subscribe(data => {
        this.router.navigateByUrl("/home/news");
      }, (err) => {
        console.error("unable to connect to server:", JSON.stringify(err));
      });

上面的代码产生以下错误:

{
 "headers":{"normalizedNames":{},
 "lazyUpdate":null,
 "headers":{}},
 "status":0,
 "statusText":"Unknown Error",
 "url":"https://sitename.com/DataServer/News",
 "ok":false,
 "name":"HttpErrorResponse",
 "message":"Http failure response for https://sitename.com/DataServer/News: 0 Unknown Error",
 "error":{"isTrusted":true}
}

我尝试从POSTMAN应用程序更新相同内容,并且工作正常。知道错误的原因吗?

预先感谢您。

cordova http ionic-framework post
1个回答
0
投票

Android 8: Cleartext HTTP traffic not permitted

内部config.xml中,您可以添加:

<application android:usesCleartextTraffic="true" />

像这样:

<platform name="android">
...
   <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
         <application android:usesCleartextTraffic="true" />
   ...
   </edit-config>
© www.soinside.com 2019 - 2024. All rights reserved.