我正在使用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应用程序更新相同内容,并且工作正常。知道错误的原因吗?
预先感谢您。
内部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>