Angular的POST请求未正常工作。

huangapple go评论65阅读模式
英文:

Angular post request doesn't work correctly

问题

我在Spring中创建了一个简单的后端,其中包含一个用于在数据库中创建新记录的POST请求,并尝试从前端调用它。
我确信后端应用程序正常运行,因为我已经在Postman中进行了测试。
在前端方面,我有一个输入框,用于插入URL,以及一个用于创建新记录的按钮。

我的服务文件中的函数:

addPhoto(url: string) {
    console.log('ciaooo');
    this.http.post("http://localhost:8080/api/photos/create", {
        url: url
    })

我的组件:

createPhoto() {
    if (!this.formCreate.valid) {
        alert("请填写所有必填字段");
    } else {
        if (this.formCreate.controls['url'].value != null) {
            console.log('hhh');
            let result = this.tservice.addPhoto(this.formCreate.controls['url'].value);
        } else {
            console.log('属性为空');
        }
    }
}

我的HTML:

<form (ngSubmit)="createPhoto()" [formGroup]="formCreate">
    <input type="text" formControlName='url'>
    <button type="submit">创建</button>
</form>

我在控制台中没有收到错误。但是函数没有产生效果。

英文:

I did a spmple backend in spring with a post that creates a new record in db and I'm trying to call it from frontend.
I'm sure that backend side application works well because I've tested it on postman.
frontend side I have an input where I insert url and a button to create a new record.

My function in service file:

addPhoto(url: string) {
        console.log(&#39;ciaooo&#39;)
        this.http.post(&quot;http://localhost:8080/api/photos/create&quot;, {
            url: url
        })

My component:

  createPhoto() {
    if (!this.formCreate.valid) {
      alert(&quot;compilare tutti i campi obbligatori&quot;)
    } else {
      //console.log(this.formCreate.controls[&#39;url&#39;].value)
      if (this.formCreate.controls[&#39;url&#39;].value != null) {
        console.log(&#39;hhh&#39;)
        let result = this.tservice.addPhoto(this.formCreate.controls[&#39;url&#39;].value)
      }else{
        console.log(&#39;prop null&#39;)
      }
    }
  }

my html:

&lt;form (ngSubmit)=&quot;createPhoto()&quot; [formGroup]=&quot;formCreate&quot;&gt;
        &lt;input type=&quot;text&quot; formControlName=&#39;url&#39;&gt;
        &lt;button type=&quot;submit&quot;&gt;Crea&lt;/button&gt;
    &lt;/form&gt;

I don't receive errors in console. But function hasn't effect

答案1

得分: 1

你需要订阅由http.post返回的Observable

this.http.post("http://localhost:8080/api/photos/create", {url}).subscribe()
英文:

You need to subscribe to the Observable returned by http.post.

this.http.post(&quot;http://localhost:8080/api/photos/create&quot;, {url}).subscribe()

答案2

得分: 0

如上所述,您需要订阅post()方法。

为了提供一些上下文:
HttpClient中的post()get()delete()等函数是RxJS Observables。Observable是一系列数据流。然而,只有在有人监听它时,这个流才会"流动"。您可以通过调用任何Observable的.subscribe()函数来监听流。

您可以像这样编写您的addPhoto方法:

// 替换 unknown 为从后端返回的正确类型
addPhoto(url: string): Observable<unknown> {
  return this.http.post("http://localhost:8080/api/photos/create", {
      url: url
  })
}

然后在组件中更新您的用法:

createPhoto(): void {
  if (!this.formCreate.valid) {
    alert("Fill in all mandatory fields");
    return;
  }

  const urlControlValue: string = this.formCreate.controls['url'].value;
  if (!urlControlValue) {
    return;
  }

  this.tservice.addPhoto(urlControlValue).subscribe((result: unknown) => console.log('result:', result));
}
英文:

As already mentioned above, you need to subscribe to the post() method.

To give some context:
The post(), get(), delete(), etc functions from the HttpClient are RxJS Observables. An Observable is a stream of data. However, the stream is only "flowing" if there is someone listening to it. You listen to a stream by calling the .subscribe() function of any Observable.

You could write your addPhoto method like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- typescript -->

// Replace unknown with the correct type which is returned from the backend
addPhoto(url: string): Observable&lt;unknown&gt; {
  return this.http.post(&quot;http://localhost:8080/api/photos/create&quot;, {
      url: url
  })

<!-- end snippet -->

And then update your usage in the component:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- typescript -->

createPhoto(): void {
  if (!this.formCreate.valid) {
    alert(&quot;compilare tutti i campi obbligatori&quot;)

    return;
  }

  const urlControlValue: string = this.formCreate.controls[&#39;url&#39;].value;
  if (!urlControlValue) {
    return;
  }

  this.tservice.addPhoto(urlControlValue).subscribe((result: unknown) =&gt; console.log(&#39;result: &#39;, result))
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月11日 04:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定