2017-06-13 48 views
-1

當我從角2應用程序發送http post請求時,我得到403禁止的錯誤。以下是form.component.ts和service.ts的代碼。403角度2禁止錯誤彈簧MVC,但在郵遞員工作

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

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()); 
} 

} 

請幫幫忙!

回答

3

也許你錯過了@CrossOrigin註釋在Spring控制器允許來自不同來源好像你是角應用發送一個請求

,因爲你是從同一產地的發送請求從郵遞員的工作Spring應用程序

例:春天服務器localhost:3000,角服務器localhost:8080,那麼你就不能沒有@crossOrigin訪問3000,但你可以直接從郵遞員

+0

訪問3000嘿感謝!有效。但有沒有其他的替代方案..它可以在應用程序級別完成,我的意思是在xmls中添加一些filters/config ?.能否請你幫忙,因爲我是新手。 –

+0

彈簧註釋是舊xml配置 的替代方案,如果您想選擇起源,您可以將起點傳遞給@CrossOrigin(origins =「http:// localhost:9000」) –

+0

因此,如果有幫助,請投票或接受我的回答 –