C# Angular4类型脚本JsonReaderException
我有一个表,其中有1条或多条记录显示给用户。提交表单时,用户选择一个或多个复选框并提交数据库中的数据。在那一刻,我来自代码调用位置。relad();以便重新加载页面并显示正确的数据 以下是来自后端web api的代码:C# Angular4类型脚本JsonReaderException,c#,.net,angular,typescript,asp.net-core,C#,.net,Angular,Typescript,Asp.net Core,我有一个表,其中有1条或多条记录显示给用户。提交表单时,用户选择一个或多个复选框并提交数据库中的数据。在那一刻,我来自代码调用位置。relad();以便重新加载页面并显示正确的数据 以下是来自后端web api的代码: [HttpGet] public async Task<object> Get() { var entries = await _entryRepository.GetEntriesByUserIdAsync(await GetUserId());
[HttpGet]
public async Task<object> Get()
{
var entries = await _entryRepository.GetEntriesByUserIdAsync(await GetUserId());
var result = _mapper.Map<IEnumerable<Entries>, IEnumerable<EntryReponse>>(entries);
return Ok(result);
}
以下是组件的代码:
import { Component, OnInit } from '@angular/core';
import { EntryService } from "../../services/entry.service";
import { MasterCodeService } from "../../services/masterCode.service";
import { SubCodeService } from "../../services/subcode.service";
import { Entry } from "../../models/entry";
import { MasterCode } from "../../models/masterCodes";
@Component({
selector: 'entry-list',
templateUrl: './entry-list.component.html'
})
export class EntryListComponent implements OnInit {
private readonly PAGE_SIZE = 100;
query: any = {
pageSize: this.PAGE_SIZE
};
masterCodes: any = [];
subCodes: any = [];
masterCode: MasterCode = new MasterCode();
entries: Entry[];
entry: Entry ={
id: [],
masterCodeId: 0,
subCodeId: 0,
accepted: 0,
rejected:0
};
columns = [
{ title: "#" },
{ title: "PO" },
{ title: "SKU" },
{ title: "Status" }
];
constructor(private entryService: EntryService,
private masterCodeService: MasterCodeService,
private subCodeService: SubCodeService) {
}
load() {
this.masterCodeService.getMasterCodes(this.query).subscribe(res => {
this.masterCodes = res;
});
this.entryService.get().subscribe(res => {
this.entries = res;
});
}
ngOnInit() {
this.load();
}
onChange(event) {
var selectedMasterCode = this.masterCodes.items.find(x => x.id == event.target.value);
this.entry.masterCodeId = selectedMasterCode.id;
this.subCodeService
.getSubCodes()
.subscribe(res => this.subCodes = res.filter(x=>x.masterCode.id == selectedMasterCode.id));
}
submit(event) {
if (event.target["noError"]) {
this.entry.accepted = 0;
this.entry.rejected = 0;
}
if (event.target["accepted"] && event.target["accepted"].checked === true ) {
this.entry.accepted = 1;
this.entry.rejected = 0;
}
if (event.target["rejected"] && event.target["rejected"].checked === true ) {
this.entry.accepted = 0;
this.entry.rejected = 1;
}
this.entryService.update(this.entry).subscribe(x => {
location.reload();
this.load();
});
}
selectSubCode(event) {
this.entry.subCodeId = event.target.value;
}
onEntryToggle(entryId, $event) {
if ($event.target.checked)
this.entry.id.push(entryId);
else {
var index = this.entry.id.indexOf(entryId);
this.entry.id.splice(index, 1);
}
}
}
调用submit后,我将遇到下一个错误:
处理请求时发生未处理的异常。JsonReaderException:解析时遇到意外字符
值:{.Path'errorMessage',第1行,位置17。
Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType ReadType)
堆栈查询Cookies标头JsonReaderException:意外字符
分析值时遇到:{.Path'errorMessage',第1行,
位置17.Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType
readType)Newtonsoft.Json.JsonTextReader.ReadAsString()
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader
读取器,JsonContract合同,bool HASCONVERTOR)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(对象
newObject、JsonReader、JSONObject合同、,
JsonProperty成员,字符串id)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader
reader,类型objectType,JsonContract合同,JsonProperty成员,
JSONCONTAINERCONTAINERCONTAINERCONTRACT,JsonProperty containerMember,
对象现有值)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader
reader,类型objectType,JsonContract合同,JsonProperty成员,
JSONCONTAINERCONTAINERCONTAINERCONTRACT,JsonProperty containerMember,
对象现有值)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader
reader,类型objectType,bool checkAdditionalContent)
Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader,
类型objectType)Newtonsoft.Json.JsonConvert.DeserializeObject(字符串
值、类型、JsonSerializerSettings设置)
Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,
JsonSerializerSettings(设置)
Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance+d_u7.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)System.Runtime.CompilerServices.TaskWaiter.GetResult()
Microsoft.AspNetCore.NodeServices.HostingModels.OutofProcess NodeInstance+d_u13.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)System.Runtime.CompilerServices.TaskWaiter.GetResult()
Microsoft.AspNetCore.NodeServices.nodeservicesiml+d_u10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)System.Runtime.CompilerServices.TaskWaiter.GetResult()
Microsoft.AspNetCore.NodeServices.nodeservicesiml+d_u10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)System.Runtime.CompilerServices.TaskWaiter.GetResult()
Microsoft.AspNetCore.SpaServices.Prerendering.PrerenderTagHelper+d_u29.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
(任务)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner+d_u0.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
(任务)
AspNetCore.\查看\u Home\u索引\u cshtml+d\u 12.MoveNext()中的
Index.cshtml
请你给我建议一下如何解决这个问题?
另外,若我不提交表单,请按F5重新加载页面,我也会遇到同样的错误
更新
以下是第一次调用的JSON:
重新加载后:
报头第一次呼叫:
重新加载后的标题:
我对某些URL路径的理解是,重新加载正在工作,而对某些URL路径则不工作。总而言之,有些路由可以手动输入到浏览器中调用,有些路由只能从链接调用,为什么?如果所有链接、所有组件、所有服务都是以相同的方式创建的,我如何判断哪里是错误的?
亲切问候,,
Danijel我对代码进行了修复
load() {
this.entryService.get().subscribe(res => {
this.entries = res;
},err=>console.log(err));
this.masterCodeService.getMasterCodes(this.query).subscribe(res => {
this.masterCodes = res;
});
}
这是我的解决方案,我想知道这对我有什么帮助,为什么我应该添加err=>console.log(err)我不知道。如果有人能向我澄清这一点,我将不胜感激
**我错了,解决方案在app.shared.ts中
在路由配置中,我添加了canActive
{路径:'entries',组件:EntryListComponent,canActivate:[AuthGuard]}
由于API是授权的,这是强制性的。**您能告诉我们它试图解析的JSON吗?我有一个更新问题。只是补充一下,我在调用GET时在后端设置了断点。在第一次调用时,它起作用了,如果我尝试重新加载页面,GET根本不会调用。对不起,除了您添加的内容之外,您可以改为检查网络吗面板,并添加正在发送到后端的实际JSON?它需要在一个失败的请求上。现在可以了吗?正如我所看到的,不同之处在于,在重新加载时,它没有调用正确的URI。
load() {
this.entryService.get().subscribe(res => {
this.entries = res;
},err=>console.log(err));
this.masterCodeService.getMasterCodes(this.query).subscribe(res => {
this.masterCodes = res;
});
}