离子6 HTTP帖子显示了电容器项目的标准名称中未知错误

发布于 2025-01-23 22:36:19 字数 1750 浏览 0 评论 0原文

我在我的离子电容器项目中打电话给我的WordPress REST JSON API。

但是我得到下图中显示的错误。

离子电容器http error

这是我的代码

const httpHeader = { // constant for http headers
  headers : new HttpHeaders({
    'Content-Type':'application/json',
    'Access-Control-Allow-Origin': '*'
  })
};

createComment(comment: Comment): Observable<any> { 
    return this.http.post('https://readymadecode.com/wp-json/wp/v2/comments/create,{
    "post":4000,
    "parent":"0",
    "author_name":"chetan",
    "author_email":"[email protected]",
    "content":"nice good article"
},httpHeader).pipe(map(this.dataExtract),catchError(this.errorHandler));
  }

private dataExtract(res: Response){ // This method extract data from the request response
    const body = res;
    return body || {};
  }

  private errorHandler(error: HttpErrorResponse){ // Method for error handler
    console.error(error.error instanceof ErrorEvent?`Error message: 
      ${error.error.message}`:`Error status: ${error.error.data.status} Body: ${error.error.message}`);
    return throwError(`${error.error.message}`);
  }

当我调用创建功能时,这 是我的代码多于。我尝试使用Cordova-Plugins-Whitelist启用CORS,但仍然显示出错误。

但是这个API在Postman中正常工作。我在Postman中使用了这一点。

url: https://www.readymadecode.com/wp-madecode.com/wp-json/ WP/V2/注释/创建 方法:发帖 身体: { “帖子”:4000, “父”:“ 0”, “ fure_name”:“ chetan”, “ fure_email”:“

请帮助我如何解决此错误。

I am calling my Wordpress REST JSON API in my Ionic Capacitor Project.

But i am getting the error shown in image below.

Ionic Capacitor HTTP Error

This is my code

const httpHeader = { // constant for http headers
  headers : new HttpHeaders({
    'Content-Type':'application/json',
    'Access-Control-Allow-Origin': '*'
  })
};

createComment(comment: Comment): Observable<any> { 
    return this.http.post('https://readymadecode.com/wp-json/wp/v2/comments/create,{
    "post":4000,
    "parent":"0",
    "author_name":"chetan",
    "author_email":"[email protected]",
    "content":"nice good article"
},httpHeader).pipe(map(this.dataExtract),catchError(this.errorHandler));
  }

private dataExtract(res: Response){ // This method extract data from the request response
    const body = res;
    return body || {};
  }

  private errorHandler(error: HttpErrorResponse){ // Method for error handler
    console.error(error.error instanceof ErrorEvent?`Error message: 
      ${error.error.message}`:`Error status: ${error.error.data.status} Body: ${error.error.message}`);
    return throwError(`${error.error.message}`);
  }

When i call the createComment function it shows error see in image above. I have tried enable CORS with cordova-plugins-whitelist but still it shows error.

But this api is working fine in postman. I am using this in postman.

URL: https://www.readymadecode.com/wp-json/wp/v2/comments/create
Method: POST
Body: {
"post":4000,
"parent":"0",
"author_name":"chetan",
"author_email":"[email protected]",
"content":"nice good article"
}

Please help how can i solve this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2025-01-30 22:36:19

尝试Google上所有可用的方法后,我可以通过简单地从API中删除Httpheader来解决此问题。

createComment(comment: Comment): Observable<any> { 
    return this.http.post('https://readymadecode.com/wp-json/wp/v2/comments/create,{
    "post":4000,
    "parent":"0",
    "author_name":"chetan",
    "author_email":"[email protected]",
    "content":"nice good article"
},httpHeader).pipe(map(this.dataExtract),catchError(this.errorHandler));
  }

after try all the methods available on google, i able to solve this issue by simply removing the httpHeader from the api.

createComment(comment: Comment): Observable<any> { 
    return this.http.post('https://readymadecode.com/wp-json/wp/v2/comments/create,{
    "post":4000,
    "parent":"0",
    "author_name":"chetan",
    "author_email":"[email protected]",
    "content":"nice good article"
},httpHeader).pipe(map(this.dataExtract),catchError(this.errorHandler));
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文