【问题标题】:How to send special characters like "++" and "#" in a request?如何在请求中发送“++”和“#”等特殊字符?
【发布时间】:2020-04-20 14:59:45
【问题描述】:

我正在尝试使用 Typescript 从 Angular 应用程序向 ASP .NET CORE API 发出请求。

当我发送请求时,它会在 SQL 数据库中搜索是否有任何具有该值的行。

问题是当我尝试发送Syntax = "C++""C#" 的请求时,它只发送这个:

/FindBySyntax?Syntax=C

而不是/FindBySyntax?Syntax=C#/File/FindBySyntax?Syntax=C++

示例:如果我尝试发送“Javascript”它的工作,它会返回给我Syntax="Javascript" 的所有行。

我想知道是否有办法在请求中发送“++”和“#”等特殊字符,我该怎么做?

selectSyntax(file: FileModel){
    this.service.formData = Object.assign({}, file);
    return this.http.get(this.service.BaseURL + '/File/FindBySyntax?Syntax=' + this.service.formData.Syntax)
    .toPromise()
            .then(res => this.service.list = res as FileModel[]);
}

【问题讨论】:

    标签: asp.net angular typescript request response


    【解决方案1】:

    您可以使用Encode URI component对所有特殊字符进行编码

    const encodedSyntax = encodeURIComponent(this.service.formData.Syntax);
    return this.http.get(this.service.BaseURL + '/File/FindBySyntax?Syntax=' + encodedSyntax)
    

    您可能必须在服务器上使用所选语言中的等效 Decode URI component 对其进行解码

    【讨论】:

      【解决方案2】:

      this.service.formData.Syntax 的内容需要用encodeURIComponent() JavaScript 函数转换。

      关于encodeURIComponent() 的文档可以在here 找到。关于 ASP .NET Core 端的解码here

      【讨论】:

        猜你喜欢
        • 2012-08-11
        • 1970-01-01
        • 2017-06-19
        • 2015-05-28
        • 2021-03-29
        • 1970-01-01
        • 2014-10-17
        • 2013-09-13
        • 1970-01-01
        相关资源
        最近更新 更多