【问题标题】:Cannot send Post Request Again after status 400 error even after correcting the request即使在更正请求后,也无法在状态 400 错误后再次发送发布请求
【发布时间】:2020-04-02 16:14:02
【问题描述】:

我有一个编辑产品页面。我正在编辑和产品。如果我使用该名称的现有产品编辑产品名称并使用 JWT 向 Java Spring REST API 提交发布请求。我有验证,它返回正确的状态 400。
但是问题是,当我更正名称字段并重新发送它时,如果没有重新加载页面,状态 400 会再次显示相同的错误消息。 这是我的提交功能:

 onSubmit() {
    this.productSubmit = true;
    if (
      this.productFormGroup.valid &&
      !this.isTableEmpty &&
      this.isFormValid &&
      !this.isCustomTableEmpty
    ) {
      this.isLoading = true;
      this.product.name = this.f.name.value;
      this.product.productCategory = this.f.category.value;
      this.product.model = this.f.model.value;
      this.product.barcode = this.f.barcode.value;
      this.product.unit = this.f.unit.value;
      this.product.manufacturer = this.f.manufacturer.value;
      this.product.cost = this.f.cost.value;
      this.product.minimumStockLevel = this.f.minStock.value;
      this.product.description = this.f.description.value;
      this.product.length=this.f.length.value;
      this.product.width=this.f.width.value;
      this.product.height=this.f.height.value;
      this.product.weight=this.f.weight.value;
       if (this.f.brand.value) {
        this.product.brand = this.f.brand.value;
      } else {
        this.product.brand = null;
      }
      if (this.f.foreignName.value) {
        this.product.foreign_name=this.f.foreignName.value;
      } else {
        let foreignName=this.f.name.value;
        this.product.foreign_name=foreignName;
      }
      if (this.f.foreignSKU.value) {
        this.product.foreign_sku=this.f.foreignSKU.value;
      } else {
        let foreignSKU=this.f.model.value;
        this.product.foreign_sku=foreignSKU;
      }
      if (this.f.preferred_supplier.value) {
        this.product.preferred_supplier=this.f.preferred_supplier.value;
      } else {
        this.product.preferred_supplier=null;
      }
      this.product.prices = this.myPriceLists;
      let id = null;
      if (this.activatedRoute.snapshot.params["id"]) {
        id = this.activatedRoute.snapshot.params["id"];
      } else {
        this.router.navigate(["/loggedIn", "product", "list"]);
      }
      this.product.productCategory = this.selectedCategory;

      this.formData.append(
        "product",
        new Blob(
          [JSON.stringify(this.product, this.appService.jsonStringifier)],
          { type: "application/json" }
        )
      );

      this.images = this.images.concat(this.product.productImages);
      for (let image of this.images) {
        this.formData.append("images", image);
      }
      this.productService.updateProduct(this.formData, id).subscribe(
        () => {
          this.productService.resetProducts();
          this.messageService.add({
            severity: "info",
            summary: "Success",
            detail: "Product Successfully Edited"
          });
          this.router.navigate(["/loggedIn", "product", "list"]);
        },
        (errorResponse : HttpErrorResponse) => {
          this.productErrors = this.appService.errorObjToMap(
            errorResponse.error.errors
          );
          this.productSuccess = errorResponse.error.success;
          this.isLoading = false;
          this.messageService.add({
            severity: "error",
            summary: "Failure",
            detail: "Product not edited."
          });
         console.log(errorResponse.error.errors[0].message);

        },
        () => {
          this.successAlert = true;
          this.isLoading = false;
        }
      );
    }
  }

* 这就是我的 REST 控制器对于这个请求的样子*

    @RequestMapping(value = "save/{id}", method = RequestMethod.POST)
    public ResponseEntity addProduct(@PathVariable("id") Integer id, @Valid @RequestPart Product product, @RequestPart MultipartFile[] images, BindingResult bindingResult, HttpServletRequest request) throws IOException {
        return saveProduct(id, product, images, bindingResult, request);
    }

除此之外一切正常。 我希望你能理解这个问题。如果您有任何问题,可以问我或任何我可能错过的内容。

【问题讨论】:

    标签: java angular typescript httprequest spring-rest


    【解决方案1】:

    您应该尝试在

    之后重新设置 messageService 对象
    console.log(errorResponse.error.errors[0].message);
    

    我不熟悉 Angular,但 React,因此从这个角度提出了一个答案。

    【讨论】:

    • 感谢您的回复。我尝试在延迟后清除消息服务,但仍然无效。
    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多