Asp.net Angular 8 httpclient如何发送JSON头
我正在尝试使用angular 8 HttpClient向ASP.net核心后端发送JSON,代码如下:Asp.net Angular 8 httpclient如何发送JSON头,asp.net,json,angular,httpclient,angular8,Asp.net,Json,Angular,Httpclient,Angular8,我正在尝试使用angular 8 HttpClient向ASP.net核心后端发送JSON,代码如下: import { HttpClient, HttpHeaders} from '@angular/common/http'; import { User } from '@/_models'; login(username, password) { return this.http.post<any>(`https://${config.apiUrl}/
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { User } from '@/_models';
login(username, password) {
return this.http.post<any>(`https://${config.apiUrl}/api/User/LoginUser`,
JSON.stringify({
"username": username,
"password": password
})
).pipe(map(user => {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
console.log(user);
this.currentUserSubject.next(user);
return user;
}));
}
}
正在发送请求
请求头包括:
POST /api/User/LoginUser HTTP/1.1
Host: (Checked_and_correct_API_URL)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: http://localhost:8080/login
Content-Type: text/plain
Content-Length: 49
Origin: http://localhost:8080
DNT: 1
Connection: keep-alive
HTTP/2.0 415 Unsupported Media Type
content-type: application/problem+json; charset=utf-8
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
date: Thu, 19 Dec 2019 13:10:01 GMT
content-length: 147
X-Firefox-Spdy: h2
响应标题为:
POST /api/User/LoginUser HTTP/1.1
Host: (Checked_and_correct_API_URL)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: http://localhost:8080/login
Content-Type: text/plain
Content-Length: 49
Origin: http://localhost:8080
DNT: 1
Connection: keep-alive
HTTP/2.0 415 Unsupported Media Type
content-type: application/problem+json; charset=utf-8
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
date: Thu, 19 Dec 2019 13:10:01 GMT
content-length: 147
X-Firefox-Spdy: h2
发送的参数为:
{"username":"jeboimarc2","password":"KUTminor1!"}
这也是正确的
有人能告诉我为什么出现不支持的媒体类型错误吗?提前感谢。传递原始json数据,不要将其转换为字符串 试着这样做:
return this.http.post<any>(`https://${config.apiUrl}/api/User/LoginUser`,{"username": username, "password": password })
返回此.http.post(`https://${config.apirl}/api/User/logiuser`,{“用户名”:用户名,“密码”:密码})
首先,感谢您的快速回答,很抱歉,假期的事情都发生了,我无法回到这里。现在我在发送JSON时得到了一个405(不允许使用方法)。不过,我认为这是由于我运行的网络浏览器,今天下午我将尝试另一个