Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Asp.net 使用Angular从webapi检索单个值_Asp.net_Angular_Typescript - Fatal编程技术网

Asp.net 使用Angular从webapi检索单个值

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

目标:检索角度中的单个值“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)
{
    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'sUP
    string
    仅允许返回字符串,
    ActionResult
    允许返回字符串或ActionResult,如
    NotFound()
    InternalServerError()
    等。