角度2弹簧MVC的403禁止错误,但在邮递员中工作 [英] 403 forbidden error for angular 2 spring mvc but working in postman
问题描述
当我从angular 2应用程序发送http post请求时,我收到403禁止错误.下面是form.component.ts和service.ts的代码.
I am getting 403 forbidden error when i am sending http post request from angular 2 application. Below is the code for form.component.ts and the service.ts.
form.component.ts
form.component.ts
`import {Component, OnInit} from '@angular/core';
import { RegisterUser} from '../Model/RegisterUser';
import { RegisterService } from
'../Services/RegisterService';
@Component({
selector:'register-user-form',
templateUrl:'/app/Htmls/Register.html',
providers : [RegisterService]
})
export class RegisterUserComponent implements OnInit{
submitted=false;
constructor(private registerUserService : RegisterService ){
}
ngOnInit(){
}
onSubmit(value:RegisterUser){
this.registerUserService.pushUser(value).subscribe(
);
console.log(value) ;
}
}`
下面是service.ts
Below is the service.ts
import { Injectable } from '@angular/core';
import { Headers,Http, Response } from '@angular/http';
import { RegisterUser} from '../Model/RegisterUser';
import 'rxjs/add/operator/map';
@Injectable()
export class RegisterService {
private headers = new Headers({'content-type': 'application/json'});
constructor(private _http : Http){}
pushUser(registerUser:RegisterUser){
console.log("submittin user "+JSON.stringify(registerUser));
const url:string="http://localhost:8080/register/saveUser";
return this._http.post(url,JSON.stringify(registerUser),
{headers:this.headers}).
map((response:Response) => response.json());
}
}
请帮助!
推荐答案
也许您在spring控制器中缺少@CrossOrigin批注,以允许来自不同来源的请求(例如您从Angular应用程序发送的请求)
maybe you are missing the @CrossOrigin annotation in your spring controller to allow requests from different origins like the one you are sending from Angular application
它可以在邮递员那里工作,因为您发送的请求与您的Spring应用程序具有相同的来源
It works from postman cause you are sending requests from the same origin as your spring application
例如:spring服务器localhost:3000,角度服务器localhost:8080,那么如果没有@crossOrigin,则无法访问3000,但是可以直接从邮递员访问3000
Example: spring server localhost:3000, angular server localhost:8080, then you cannot access 3000 without the @crossOrigin but you can access 3000 directly from the postman
这篇关于角度2弹簧MVC的403禁止错误,但在邮递员中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!