【发布时间】:2019-10-06 04:07:06
【问题描述】:
我不确定交易是什么,希望有人能给我一些帮助!
我正在尝试对后端 API 进行 POST,但我收到了 415 状态代码,因为内容类型正在作为“文本/纯文本”发送,但此端点需要应用程序/json。我想可能是 API,但 POST 在 PostMan 中工作得很好(见下面的截图)。
我尝试在请求标头中手动将 content-type 设置为 application/json,但我只得到 500 状态代码(见下面的屏幕截图)。 API 的所有其他端点都工作得很好,但他们期待“文本/纯文本”......非常感谢任何帮助!
我只是设置了一个简单的按钮来进行 POST:
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.less']
})
export class HomeComponent implements OnInit {
constructor(private http: HttpClient) { }
ngOnInit() {}
onClickMe( ) {
// I'VE TRIED ALL THESE DIFFERENT WAYS ALSO
/*
const headers = new HttpHeaders({
'Content-Type': 'application/json'
});
const httpHeaders = new HttpHeaders();
httpHeaders.append('Content-Type', 'application/json');
const options = {
headers: httpHeaders
};
*/
const httpHeaders = new HttpHeaders().set(
'Content-Type',
'application/json'
);
const uNameObj = {
username: "asdf"
};
const jsonStr = JSON.stringify(uNameObj);
let existsCheck: boolean;
this.http
.post('http://localhost:8080/myapp/user/username',
'{"username": "asdf"}',
{
observe: 'response',
headers: httpHeaders
})
.subscribe(
responseData => {
if (responseData.status === 200) {
existsCheck = true;
} else {
existsCheck = false;
}
},
error => {
console.log('Error with post button', existsCheck);
}
);
}
}
【问题讨论】:
-
您尝试过:
this.http.post('http://localhost:8080/myapp/user/username', jsonStr,以及您的 Post Action Method 的样子吗? -
是的,我试过了。抱歉,我的意思是说。
-
angular.io/guide/http#making-a-post-request 使用接口并发布uNameObj
-
嘿安德鲁,我不太确定你的建议是什么
标签: angular post httpclient content-type angular8