尝试提交表单时出现HTTP发布请求错误

卡夫克斯

我已经创建了一个Web应用程序,该应用程序可以创建足球俱乐部并生成足球比赛。它的一个功能是,当用户在GUI上输入随机日期时,该程序应显示当天的所有比赛。为此,我在HTML文件中创建了一个表单,并使用HTTP post request将数据发送到另一个项目(2个不同端口)中的后端localhost:4000-Angular GUI localhost:8082-Java Spring Boot应用程序(后端)

这是我的日期检查部分的代码!

---- HTML文件-----

<div id="dateContainer"><form #datePost="ngForm" (ngSubmit)="onSubmit(datePost.value)" >
<input type="text" name="date" ngModel placeholder="12/12/2012">
<button type="submit">Find</button>
</form>
</div>

----- TS文件----

import { Component, OnInit } from '@angular/core';

import{HttpClient, HttpClientModule} from '@angular/common/http';

@Component({

  selector: 'app-date',

  templateUrl: './date.component.html',

  styleUrls: ['./date.component.css']

})
export class DateComponent implements OnInit {

  onSubmit(data){

    this.http.post('http://localhost:8082/dateReq',data)

    .subscribe((result)=>{

      console.warn("result",result)

    })
  
  }
  constructor(private http:HttpClient) { }

  ngOnInit(): void {
  }

}

----- Java -----

@CrossOrigin("http://localhost:4200")

@GetMapping("/dateReq")

public ArrayList<String> getDates(){

    return PremiereLeagueManager.dateCheck;

}

当我在GUI中输入日期并单击“查找”按钮时,会在控制台上收到这些错误

Access to XMLHttpRequest at 'http://localhost:8082/dateReq' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
:8082/dateReq:1 

Failed to load resource: net::ERR_FAILED
core.js:5967 

ERROR HttpErrorResponse
defaultErrorLogger @ core.js:5967
奥拉鲁·阿丽娜(Olaru Alina)

为什么在Java中有一个GetMapping,而在Angular中为什么使用http.post?获取请求也没有参数。尝试更改您的ts文件:

onSubmit(数据){

this.http.post('http://localhost:8082/dateReq',data)

.subscribe((result)=>{

  console.warn("result",result)

})

}

并在Java中:

@CrossOrigin("http://localhost:4200")

@PostMapping("/dateReq")

public ArrayList<String> getDates(){

    return PremiereLeagueManager.dateCheck;

}

另外,我强烈建议您使用反应式表单,因为它们更好,您可以在此处查看详细信息:https : //angular.io/guide/reactive-forms

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试将新问题发布到GitHub存储库时出现错误“ 400错误的请求”

尝试发布大型JSON数据时出现错误的请求错误

当我尝试提交HTML表单时显示错误“无法发布”

在Spring MVC中使用modelAttribute进行表单提交时在Spring MVC中上传多个文件时出现400错误的请求

尝试发布工件时出现错误

尝试发布http时出现角度错误的数据

提交表单时出现Spring Boot错误

尝试更改最大请求长度时出现HTTP错误500.19

提交JQuery表单发布请求时页面不起作用

发布IFormFile时出现错误请求(400)

(400)尝试将简单值发布到API时出现错误请求

尝试在Shopify上发布请求时,请求给出400错误

尝试执行表单验证时出现错误

通过jquery提交表单时没有ajax发布请求

尝试从vue.js中的HTTP请求推送到数组时出现错误

当我尝试提交HTML表单时显示错误“无法发布”

提交表单时出现验证错误

提交表单时出现404错误

发出HTTP请求时出现OpenSSL错误

400发送HTTP发布请求以从身份验证代码获取令牌时出现错误请求?

提交引导表单时出现jQuery错误

使用Django表单提交时出现错误

尝试使用SolidWorks VBA发出HTTP请求时出现奇怪的错误

尝试提交表单提交请求的尝试失败

当我尝试在节点中执行https发布请求时,出现getaddrinfo ENOTFOUND错误

HTTP请求失败!尝试从youtube获取内容时出现HTTP / 1.0 400错误请求

验证 symfony 表单以提交 POST 请求时出现问题

HTTP 状态 400 – 尝试将日期保存到数据库时出现错误请求

尝试执行简单的http请求时出现JS错误错误