【发布时间】:2021-02-21 14:48:41
【问题描述】:
我有以下问题。 我有一个 API 所在的服务器,我将请求发送到注册端点,但作为响应,我收到 400 Bad Request 错误,说明必须填写姓名、姓氏、电子邮件等。问题是他们被填满了。我不再想念想法了。我的代码:
import {Component} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Brak tytułu';
constructor(private http: HttpClient) {
}
registerUser() {
const config = {headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')};
const user: User = {
name: 'Pawel',
surname: 'Tester',
email: 'tester@wp.pl',
password: 'loremipsum12',
password_confirm: 'loremipsum12',
event_id: 1,
};
this.http.post('https://myapiserver', user, config).subscribe((res) => {
console.log(res);
console.log(user);
},
error => {
console.log(error);
console.log(user);
});
}
}
interface User {
name: string;
surname: string;
email: string;
password: string;
password_confirm: string;
event_id: number;
}
这段代码运行良好,但他是 angularJS
// User create
$scope.createUser = function()
{
var create_req = $rootScope.reqDefaults;
create_req.method = 'POST';
create_req.url = $rootScope.api+'/admin/user';
create_req.data = $httpParamSerializerJQLike($scope.current);
create_req.transformRequest = angular.identity;
// Users list
$http(create_req)
.then(function(response){
$location.path('/users');
$.notify({
title: '<strong>Użytkownik utworzony.</strong>',
message: $scope.current.name+' '+$scope.current.surname+' (ID: '+response.data.id+')'
},{
type: 'success'
});
return true;
}, function(response){
$rootScope.rspError(response.data);
return false;
});
};
如果我从 Postman 向注册端点发送请求,一切正常,我的用户注册正确:(我将内容类型设置为 application/x-www-form-urlencoded。
【问题讨论】:
-
您使用的是哪个网络服务器?
-
你好,服务器是如何实现的?使用浏览器的开发工具,您应该能够检查消息的真实内容。你能仔细检查一下吗?
-
我在 VPS 上使用 Symfony 3 的真实 API
标签: angular