Angular HTTP POST请求抛出net :: ERR_HTTP2_PROTOCOL_ERROR错误 [英] Angular HTTP POST Request throwing net::ERR_HTTP2_PROTOCOL_ERROR error

查看:274
本文介绍了Angular HTTP POST请求抛出net :: ERR_HTTP2_PROTOCOL_ERROR错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的API和 POST 路由,如下所示,

I have my own API and a POST route working as follows,

服务器

Server

//  Handling CORS with a simple lazy CORS
$app->options('/{routes:.+}', function ($request, $response, $args) {
    return $response;
});
$app->add(function ($req, $res, $next) {
    $response = $next($req, $res);
    return $response
        ->withHeader('Access-Control-Allow-Origin', '*')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization, application/json')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST')
        ->withHeader('Content-Type','application/json')
        ->withHeader('X-Powered-By','Mercurial API');

});

...

$app->post('/api/log', function( Request $request, Response $response){
    
    $category = $request->getParam('category');
    $value = $request->getParam('value');
     
    return logQuery($category, $value, $response);

});

当我发送从服务器响应细其它源的HTTP POST请求,

When i am sending a HTTP POST Request from other sources the server is responding fine, Kindly click here to see the example

类别:"SOME CAT"

category:"SOME CAT"

值:"SOME VAL"

value:"SOME VAL"

但是当我通过Angular应用发送相同内容时,

But when i sending the same through my Angular App,

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

...

  public putLog(category: string, value: string) {
    //  You can add new argument in header like,
    //  httpOptions.headers = httpOptions.headers.set('Authorization', 'my-new-auth-token');

    const body = JSON.stringify({ category: category, value: value });

    console.log(body);
    return this.http.post<any>(this.apiUrl + '/log', body, httpOptions)
      .subscribe(
        data => {
          console.log('PUT Request is successful.');
        },
        (err: HttpErrorResponse) => {
          if (err.error instanceof Error) {
            console.log('Client-side error occured.');
          } else {
            console.log('Server-side error occured.');
          }
        });

    }
  }

我遇到以下错误.

{"category":"message","value":"asdasd"}
Server-side error occured.
OPTIONS https://sizilkrishna.000webhostapp.com/api/public/api/log net::ERR_HTTP2_PROTOCOL_ERROR

我在做什么错了?

推荐答案

您的api需要HttpParams,因此您应该将params设置为params而不是body:

Your api expects HttpParams, so you should set your params as params and not body:

const params = new HttpParams().set("category", category).set("value", value);

const httpOptions = {
  headers: new HttpHeaders({
    'Accept': 'application/json',
  }),
  params: params
};

,然后将主体设为 null :

return this.http
  .post<any>(
    this.apiUrl + "/log",
    null,
    httpOptions
  )
  // .....

这似乎很好用: STACKBLITZ

That seems to work just fine: STACKBLITZ

这篇关于Angular HTTP POST请求抛出net :: ERR_HTTP2_PROTOCOL_ERROR错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆