简体   繁体   English

尝试运行发布请求时出现Angular 8未知错误

[英]Angular 8 Unknown Error while trying to run post request

I am new to angular and cannot figure out why I am getting this unknown error using this code.我是 angular 的新手,无法弄清楚为什么使用此代码会出现此未知错误。 There are no errors in my API.我的 API 中没有错误。 There are no errors in my ng server window.我的 ng 服务器窗口中没有错误。 I am at a lost.我不知所措。 I am running in angular 8 with a Python Api running Falcon我正在使用运行 Falcon 的 Python Api 以 angular 8 运行

TS: TS:

import {FormBuilder, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-create-user',
  templateUrl: './create-user.component.html',
  styleUrls: ['./create-user.component.scss']
})
export class CreateUserComponent implements OnInit {
  userForm: any;

  onSubmit() {
    if (this.userForm.valid) {
      alert('User form is valid!!' );
      this.http.post('http://server:port/CreateUser', this.userForm.value)
      .subscribe((response) => {
        console.log('repsonse ', response);
      });
    } else {
      alert('User form is not valid!!');
    }
  }

  constructor(private formBuilder: FormBuilder, private http: HttpClient) { }
  ngOnInit() {
    this.userForm = this.formBuilder.group({
      firstname: ['', Validators.required],
      lastname: ['', Validators.required ],
      email: ['' , [Validators.required, Validators.email]],
      password: ['' , Validators.required],
      title: ['' , Validators.required],
      company: ['' , Validators.required]
    });
  }

}

HTML HTML

<form [formGroup]="userForm" (ngSubmit)="onSubmit()" class="text-center border border-light p-5">

  <p class="h4 mb-4">Sign up</p>

  <div class="form-row mb-4">
      <div class="col">
       First-Name:   <input type="text" formControlName="firstname" id="defaultRegisterFormFirstName" class="form-control" placeholder="First name">
      </div>
      <div class="col">
         Last-Name <input type="text" formControlName="lastname" id="defaultRegisterFormLastName" class="form-control" placeholder="Last name">
      </div>
  </div>
  <div class="row">
   Email:  <input type="email" formControlName="email" id="defaultRegisterFormEmail" class="form-control mb-4" placeholder="E-mail">
  </div>
  <div class="row">
    Password:  <input type="password" formControlName="password" id="defaultRegisterFormEmail" class="form-control mb-4" placeholder="Password">
   </div>
  <div class="row">
    Title: <input type="text" formControlName="title" id="defaultRegisterFormTitle" class="form-control mb-4" placeholder="Title">
  </div>
  <div class="row">
  Company: <input type="text" formControlName="company" id="defaultRegisterFormCompany" class="form-control mb-4" placeholder="Company">
  </div>
  <div class="row">
    <button class="btn btn-info my-4 btn-block" type="submit">Sign in</button>
</div>
  <hr>

</form>

Response Message:响应消息:

Http failure response for http://server:port/CreateUser: 0 Unknown Error

Json Request I know should work:我知道 Json 请求应该有效:

"password": "234wfw5f",
"firstname": "fn",
"lastname": "ln",
"company": "company",
"title": "title"}

serverport应该是变量吗?

After Lots of googling I figured out it was because of Cors, i was just not getting a good error message.经过大量谷歌搜索后,我发现这是因为 Cors,我只是没有收到好的错误消息。 So if this occurs for anyone else make sure to check to see if it is a Cors error因此,如果其他人发生这种情况,请务必检查是否是 Cors 错误

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

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