首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 9获取api Http失败响应404错误

Angular 9获取api Http失败响应404错误
EN

Stack Overflow用户
提问于 2020-06-05 05:05:24
回答 2查看 1.9K关注 0票数 0

我从UI调用REST API,但出现以下错误

错误代码: 404消息:https://localhost:5001/api/SampleDataSource/GetDetailsById?id=101 404 OK的Http失败响应

代码语言:javascript
复制
query(queryStringParameters?: any): Observable < object > {
      private headers = new HttpHeaders(); 

     this.headers = this.headers.append('X-Requested-With', 'XMLHttpRequest');
      this.headers = this.headers.append('Content-Type', 'application/json');
      this.headers = this.headers.append('Accept', 'application/json');
      this.headers = this.headers.append('Access-Control-Allow-Headers', 'Content-Type');

     return this.http.get(this.BASE_URL + '?' + queryStringParameters, { headers: this.headers }).pipe(catchError(this.handleError));
}

c#控制器

代码语言:javascript
复制
[Route("api/[controller]")]
        public class SampleDataSourceController : Controller
        {
         [HttpGet("GetDetailsById")]
                public IActionResult GetDetailsById([FromQuery] string id)
                {

                    return Ok(null);
                }
    }

常规:

代码语言:javascript
复制
Request URL: https://localhost:5001/api/SampleDataSource/GetDetails?id=101
Request Method: GET
Status Code: 404 
Remote Address: [::1]:5001
Referrer Policy: strict-origin-when-cross-origin

响应头:

access-control-allow-control:*

代码语言:javascript
复制
connection: keep-alive
content-length: 174
content-security-policy: default-src 'none'
content-type: text/html; charset=utf-8
date: Fri, 05 Jun 2020 15:02:42 GMT
server: Kestrel
status: 404
x-content-type-options: nosniff
x-powered-by: Express

请求头部:

代码语言:javascript
复制
:authority: localhost:5001
:method: GET
:path: /api/SampleDataSource/GetDetails?id=101
:scheme: https
accept: application/json
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,fr;q=0.8,te;q=0.7
access-control-allow-headers: Content-Type
cache-control: no-cache
content-type: application/json
cookie: ai_user=d3TQ|2020-06-04T15:35:45.072Z; ai_session=gU3qi|1591367714431|1591367843063.445
pragma: no-cache
referer: https://localhost:5001/
request-id: |ed257af3e5b34db4be330d4be3b41449.ac4bec56ee484ece
sec-ch-ua: "\\Not;A\"Brand";v="99", "Google Chrome";v="85", "Chromium";v="85"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4158.4 Safari/537.36
x-requested-with: XMLHttpRequest
EN

回答 2

Stack Overflow用户

发布于 2020-06-05 05:09:38

404 -是标准错误,请求的资源找不到。检查您的api URL,看起来您的url是错误的(可能是拼写错误?)此外,要验证url,请检查Chrome中的网络选项卡以查看真实的URL

票数 1
EN

Stack Overflow用户

发布于 2020-06-05 12:26:23

您可以像这样在HttpParams中添加queryStringParameters

代码语言:javascript
复制
    query(queryStringParameters?: any): Observable < object > {
        private headers = new HttpHeaders(); 

        this.headers = this.headers.append('X-Requested-With', 'XMLHttpRequest');
        this.headers = this.headers.append('Content-Type', 'application/json');
        this.headers = this.headers.append('Accept', 'application/json');
        this.headers = this.headers.append('Access-Control-Allow-Headers', 'Content-Type');
        const httpParams = new HttpParams().set('page', queryStringParameters.page).set('sort', queryStringParameters.sort);

         return this.http.get(this.BASE_URL{ headers: this.headers, params: httpParams }).pipe(catchError(this.handleError));
     }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62204121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档