错误:SyntaxError:

我正在加载嵌套对象,但传入的数据没有正确关闭json“ [”: 有人可以帮忙,我无法检索数据。

  

[{“ Id”:1,“条形码”:“ N001”,.....,“ ProductSubCategoryID”:1,“ SellerId”:0,“ SellerName”:null,“ ProductSubCategory”:{“ ProductSubCategoryID “:1,” SubCategoryName“:”乳液“,”产品“:[”

public class Product
{
    [Key]
    public int Id { get; set; }
    .
    .
    [ForeignKey("ProductSubCategoryID")]
    public virtual ProductSubCategory ProductSubCategory { get; set; }
}

public class ProductSubCategory
{
    [Key]
    public int ProductSubCategoryID { get; set; }
    [Required]
    [Column(TypeName = "nvarchar(100)")]
    public string SubCategoryName { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}
    // GET: api/Product
    public async Task<Object> GetProducts()
    {
        return Ok(await _context.Products.Include(p => p.ProductSubCategory).ToListAsync());
    }

readonly BaseURI = 'http://localhost:54277/api/Product';

  private   product$: Observable<Product[]>;

  // Get All Products
  getProducts(): Observable<Product[]> {
    if (!this.product$) {
      this.product$ = this.http.get < Product[] > (this.BaseURI + '/GetProducts').pipe(shareReplay());
    }
    return this.product$;
  }
ll8039 回答:错误:SyntaxError:

问题出在序列化中。由于循环引用,ASP.Net Core无法正确序列化为JSON。产品具有类型为ProductSubCategory的属性,并且产品SubCategory再次具有类型为Product的属性,这会导致循环引用。

因此ProductABC具有ProductSubCategoryABC,该产品又具有产品集合并具有ProductABC。

为避免循环引用,请修改您的startup.cs并指定options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore

services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });
本文链接:https://www.f2er.com/3109922.html

大家都在问