简体   繁体   English

Angular POST 请求在命中 spring 引导端点时不起作用

[英]Angular POST request is not working while hitting spring boot endpoint

I'm new to angular, and it's creating some confusion for me, as I'm unable to figure out the cause of REST POST request failure.我是 angular 的新手,这让我有些困惑,因为我无法弄清楚 REST POST 请求失败的原因。

I'm developing an application by using Angular as frontend and Spring as backend.我正在通过使用 Angular 作为前端和 Spring 作为后端来开发应用程序。

Here is my spring POST endpoint:这是我的 spring POST 端点:

@PostMapping("/memes")
    public ResponseEntity<MemeEntity> memeStreamAdd(@RequestBody Meme request) {
        return ResponseEntity.ok().body(memeService.addMeme(request.getName(), request.getUrl(), request.getCaption()));
}

and MemeEntity.java和 MemeEntity.java

public class MemeEntity {

    @Id
    @GeneratedValue
    private Long memeId;

    @NonNull
    private String name;

    @NonNull
    private String url;

    @NonNull
    private String caption;

}

And here is the angular POST request to hit the above spring endpoint:这是 angular POST 请求,用于访问上述 spring 端点:

  public addMeme(meme) {
    return this.http.post("http://localhost:8081/memes",meme,{responseType:'text' as 'json'}); 
  }

And meme object Meme.ts和 meme object Meme.ts

export class Meme{
    constructor(
        name:string, 
        caption:string, 
        url:string
    ){}
}

This is the form I'm using to hit the post request.这是我用来发送帖子请求的表格。


    <form>
      <fieldset>

          <div class="form-group">
              <label for="name"><span class="req">* </span> Name: <small>This will be your login user
                      name</small> </label>
              <input required type="text" [(ngModel)]="meme.name" name="name" id="name"/>
          </div>

          <div class="form-group">
              <label for="caption"><span class="req">* </span> caption: </label>
              <input class="form-control" type="text" [(ngModel)]="meme.caption" name="caption" id="txt"/>
              <div id="errFirst"></div>
          </div>
          <div class="form-group">
              <label for="url"><span class="req">* </span> Experience : </label>
              <input class="form-control" type="text" [(ngModel)]="meme.url" name="url"/>
              <div id="errLast"></div>
          </div>

      
          <div class="form-group">
              <input class="btn btn-success" type="submit" name="submit_reg" value="Meme" (click)="registerNow()">
          </div>
      </fieldset>
  </form>

When I'm clicking on submit, it's hitting the registerNow() method, but the request is failing as it's visible in the inspect element/browser developers tool.当我单击提交时,它正在访问 registerNow() 方法,但请求失败,因为它在检查元素/浏览器开发人员工具中可见。

Any help/tip would be very appreciated.任何帮助/提示将不胜感激。

Thank you.谢谢你。

I would expect your Spring Boot application not allowing cross origin requests and that's why you would not get any data.我希望您的 Spring 引导应用程序不允许跨源请求,这就是您不会获得任何数据的原因。 Did you alread allow CORS?您是否已经允许 CORS? https://spring.io/guides/gs/rest-service-cors/ https://spring.io/guides/gs/rest-service-cors/

What is the concrete error message in the browser?浏览器中的具体错误消息是什么? Is there any log on the server and did you enable trace logging on the server?服务器上是否有任何日志,您是否在服务器上启用了跟踪日志记录?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM