Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .NET Core 2.1使用React模板-意外令牌<;使用“时,位置0处的json”;获取();_Javascript_Reactjs_React Router_Asp.net Core 2.1 - Fatal编程技术网

Javascript .NET Core 2.1使用React模板-意外令牌<;使用“时,位置0处的json”;获取();

Javascript .NET Core 2.1使用React模板-意外令牌<;使用“时,位置0处的json”;获取();,javascript,reactjs,react-router,asp.net-core-2.1,Javascript,Reactjs,React Router,Asp.net Core 2.1,我正在尝试使用.NETCore2.1和React模板来设置一个基本的CRUD应用程序。我有一个SQL Server数据库,它有两个表:tblEmployee,tblCities 我已经成功地使用Scaffold DbContext创建了我的模型。我将我的连接字符串添加到appsettings.json 在我的Models文件夹中,我添加了一个带有一些函数的DataAccessLayer.cs(class)来访问我的上下文 例如: private readonly CoreReactAppCont

我正在尝试使用.NETCore2.1和React模板来设置一个基本的CRUD应用程序。我有一个SQL Server数据库,它有两个表:
tblEmployee
tblCities

我已经成功地使用
Scaffold DbContext
创建了我的模型。我将我的连接字符串添加到
appsettings.json

在我的Models文件夹中,我添加了一个带有一些函数的
DataAccessLayer.cs
(class)来访问我的上下文

例如:

private readonly CoreReactAppContext db=new CoreReactAppContext();
公共IEnumerable GetAllEmployees()
{
尝试
{
var employees=db.TblEmployee.ToList();
返回员工;
}
抓住
{
投掷;
}
}
由此,我有了一个
EmployeeController.cs
,用于返回数据

例如:

DataAccessLayer对象员工=新建DataAccessLayer();
[HttpGet]
[路线(“api/员工/索引”)]
公共JsonResult索引()
{
返回新的JsonResult(objemployee.GetAllEmployees());
}
这主要是我的数据API

对于我的前端代码,我有一个
FetchEmployee.js
,它获取我的所有员工并将他们呈现在一个简单的html表中

例如:

导出类FetchEmployee扩展组件{
displayName=FetchEmployee.name
建造师(道具){
超级(道具);
this.state={empList:[],load:true};
fetch('api/Employee/Index'{
方法:“get”,
标题:{'Content-Type':'application/json'}
})
。然后(响应=>{
json();
})
。然后(数据=>{
this.setState({empList:data,load:false});
}).catch(函数(错误){
log('Something…',error);
});
this.handleDelete=this.handleDelete.bind(this);
this.handleEdit=this.handleEdit.bind(this);
}
render(){
让内容=this.state.loading
加载

:FetchEmployee.renderEmployeeTable(this.state.employist); 返回( 员工数据 此组件演示如何从服务器获取员工数据

创造新的

{contents} ); } //处理用户对员工的删除请求 handleDelete(id){ fetch('api/Employee/Delete/'+id{ 方法:“删除” })。然后(数据=>{ 这是我的国家( { empList:this.state.empList.filter((rec)=>{ 返回(rec.employeeId!=id); }) }); }); } //处理用户对员工的编辑请求 handleEdit(id){ this.props.history.push('/employee/edit/'+id); } 静态渲染部署表(empList){ 返回( 员工标识符 名称 性别 部门 城市 {empList.map(emp=> {emp.employeeId} {emp.name} {emp.gender} {行政管理部} {emp.city}

你能帮我弄清楚这个吗?你知道我为什么从控制器返回
HTML

[编辑]

当我在我的
fetch()
的标题中添加了
“Accept”:“application/json”
,我收到一个404通知:

Cannot GET api/Employee/Index

您遇到的一个问题是,您没有在承诺链中传播json结果:

.then(...)
.then(response => {
  response.json(); // <-- missing a return statement or you can eliminate the {}
})
.then(...)
通常,出现给定错误消息的常见原因是接收HTML但需要JSON

Unexpected token '<' json at position 0

意外标记“我删除了{}(仍然有404),但您是否有解决方案或解释为什么我返回
HTML
而不是
JSON
?我是否无法访问我的数据库?检查您的请求和响应的标题,验证内容类型是否为正确的JSON,确保您的端点确实返回JSON。可能使用curl或Postman直接查询它,以便您可以隔离将问题转移到后端或前端。我尝试从Visual Studio 2017启动我的IIS Express,在Postman中输入
URL+/api/Employee/Index
,它返回我
HTML
。我的
public/Index.HTML
默认值
HTML
代码…这就是你的问题。你的端点返回HTML,但你正在解析它是JSON。问题似乎出现在后端,因此出现在.NET领域,而不是JavaScript。是的,我只是不确定到底是什么导致了问题!我的
AccessLayer
返回一个
IEnumerable
,而我的
控制器
返回的是JSON:
返回JSON(objeemployee.GetAllEmployees();
Unexpected token '<' json at position 0