Asp.net 使用Angular从webapi检索单个值
目标:检索角度中的单个值“test” 问题:我收到一条错误消息 错误:SyntaxError:JSON中JSON.parse()位置1处的意外标记e 我缺少什么语法Asp.net 使用Angular从webapi检索单个值,asp.net,angular,typescript,Asp.net,Angular,Typescript,目标:检索角度中的单个值“test” 问题:我收到一条错误消息 错误:SyntaxError:JSON中JSON.parse()位置1处的意外标记e 我缺少什么语法 ASP.NET // https://localhost:44353/api/Jobadvertisement/VVValidate/4 [HttpGet("VVValidate/{id:int}")] public ActionResult<String> VVValidate(int id) { retur
ASP.NET
// https://localhost:44353/api/Jobadvertisement/VVValidate/4
[HttpGet("VVValidate/{id:int}")]
public ActionResult<String> VVValidate(int id)
{
return "test";
}
您需要将
ActionResult
更改为string
:
[HttpGet("VVValidate/{id:int}")]
public string VVValidate(int id)
{
return "test";
}
您需要将
ActionResult
更改为string
:
[HttpGet("VVValidate/{id:int}")]
public string VVValidate(int id)
{
return "test";
}
请尝试在Angular中使用httpOptions,并执行以下操作:
const url = environment.url;
let asdfasdf2 = url + 'api/Jobadvertisement/VVValidate/' + "4";
var dsfsdssf = this.http.get(asdfasdf2, { responseType: 'text' }).subscribe(data => { console.log(data); });
请尝试在Angular中使用httpOptions,并执行以下操作:
const url = environment.url;
let asdfasdf2 = url + 'api/Jobadvertisement/VVValidate/' + "4";
var dsfsdssf = this.http.get(asdfasdf2, { responseType: 'text' }).subscribe(data => { console.log(data); });
Angular的默认情况下需要JSON数据,这就是为什么从API返回纯文本时会出现JSON反序列化错误
您需要将responseType:'text'
作为选项传递给get
方法,以防止HttpClient
将数据视为JSON:
httpClient.get("url", { responseType: 'text' });
您可以在中找到有关此的更多信息
尽管您的API代码可以正常工作,但我想指出两件事:
字符串
并删除操作结果
,因为它不是必需的string
类型,而不是System.string
。有关详细说明,请参阅[HttpGet("VVValidate/{id:int}")]
public string VVValidate(int id)
{
return "test";
}
Angular的默认情况下需要JSON数据,这就是为什么从API返回纯文本时会出现JSON反序列化错误
您需要将responseType:'text'
作为选项传递给get
方法,以防止HttpClient
将数据视为JSON:
httpClient.get("url", { responseType: 'text' });
您可以在中找到有关此的更多信息
尽管您的API代码可以正常工作,但我想指出两件事:
字符串
并删除操作结果
,因为它不是必需的string
类型,而不是System.string
。有关详细说明,请参阅[HttpGet("VVValidate/{id:int}")]
public string VVValidate(int id)
{
return "test";
}
使用ActionResult和string有什么不同?使用ActionResult和string有什么不同?使用ActionResult和string有什么不同?@What'sUP
string
只允许返回字符串,ActionResult
允许返回字符串或ActionResult,如NotFound()
,InternalServerError()
等。使用ActionResult和string有什么不同?@What'sUPstring
仅允许返回字符串,ActionResult
允许返回字符串或ActionResult,如NotFound()
,InternalServerError()
等。