微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular http 为现有且正确的 URL 设置了 404 !!!来自 json 服务器

如何解决Angular http 为现有且正确的 URL 设置了 404 !!!来自 json 服务器

我第一次尝试使用响应式表单来应用 Angular HTTP put。我的问题是当我应用以下代码时,我收到 404 错误。该错误指示未找到服务器 URL (XHR PUT http://localhost:3000/Feedback)。当我单独使用 URL 时,它会正常带来数据,来自 JSON 服务器的消息是 200 (GET /Feedback 200 28.207 ms - 200)。这意味着 URL 没有问题!!!但是,在 HTTP put 的情况下还是得到 404

JSON 服务器结构(我需要放入“反馈”:[])

{
  "dishes": [ ...
  ],"promotions": [ ...
  ],"leaders": [ ...
  ],"Feedback": [
   {
      "firstname": "Ali","lastname": "Sleam","telnum": "123123123","email": "Ali@gmail.com","agree": false,"contacttype": "None","message": "This is my message"
   }
  ]
}

Feedback.service.ts

...
export class FeedbackService {

  constructor(private http: HttpClient,private processHTTPMsgService: ProcessHTTPMsgService) { }

    submitFeedback(FeedBack: Feedback): Observable<Feedback> {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
      })
    };
    return this.http.put<Feedback>('http://localhost:3000/Feedback',FeedBack,httpOptions);
  }

}

component.ts

...
FeedbackForm: FormGroup;
Feedback: Feedback;
contactType = ContactType;
...
constructor(private fb: FormBuilder,private FeedbackService: FeedbackService) {
  this.createForm();
}
...
createForm() {
  this.FeedbackForm = this.fb.group({
    firstname: ['',[Validators.required,Validators.minLength(2),Validators.maxLength(25)] ],lastname: ['',telnum: ['',Validators.pattern] ],email: ['',Validators.email] ],agree: false,contacttype: 'None',message: ''
  });
}
...
onSubmit() {
  this.Feedback = this.FeedbackForm.value;

  this.FeedbackService.submitFeedback(this.Feedback) // <--- add to the server (put)
  .subscribe(Feedback => { this.Feedback = Feedback; },errmess => { this.Feedback = null; this.errMess = <any>errmess; });
}

component.html

<form novalidate [formGroup]="FeedbackForm" #fform="ngForm" (ngSubmit)="onSubmit()">
  ...
  <button type="submit" mat-button class="background-primary text-floral-white">Submit</button>
</form>

更新——反馈类

export class Feedback {
    firstname: string;
    lastname: string;
    telnum: number;
    email: string;
    agree: boolean;
    contacttype: string;
    message: string;
};

export const ContactType = ['None','Tel','Email'];

解决方法

由于 URL 作为 GET 请求工作,而不是使用 http.put,请尝试使用 http.get
这与 Angular 无关。您正在使用 PUT Request 访问在服务器中公开为 GET Request 的 URL。
由于服务器不知道 PUT 请求,它发送 404。

,

我终于找到了解决这个问题的方法,基于答案Here

JSON 服务器需要一个 id,而我的类没有包含一个 id。为 id 分配一个值并不重要,但必须至少将其包含在类中,如下所示。

export class Feedback {
    id: number;
    firstname: string;
    ...
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?