我从UI调用REST API,但出现以下错误
错误代码: 404消息:https://localhost:5001/api/SampleDataSource/GetDetailsById?id=101 404 OK的Http失败响应
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#控制器
[Route("api/[controller]")]
public class SampleDataSourceController : Controller
{
[HttpGet("GetDetailsById")]
public IActionResult GetDetailsById([FromQuery] string id)
{
return Ok(null);
}
}常规:
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:*
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请求头部:
: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发布于 2020-06-05 05:09:38
404 -是标准错误,请求的资源找不到。检查您的api URL,看起来您的url是错误的(可能是拼写错误?)此外,要验证url,请检查Chrome中的网络选项卡以查看真实的URL

发布于 2020-06-05 12:26:23
您可以像这样在HttpParams中添加queryStringParameters
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));
}https://stackoverflow.com/questions/62204121
复制相似问题