2017-03-09 44 views
0

我想創建一個CRUD應用程序,對於選擇操作一切正常但我不能創建一個新的Personne,通過發送表單的值彈簧控制器 這是我的應用程序,components.ts不能發送角度2的值到彈簧控制器使用休息

 import { personne } from './personne'; 
    import { StylesCompileResult } from '@angular/compiler'; 
    import { Component } from '@angular/core'; 
    import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
    import { Observable } from 'rxjs'; 
    import 'rxjs/add/operator/map'; 

    @Component({ 
     selector: 'app-root', 
     templateUrl: './app.component.html', 
     styleUrls: ['./app.component.css'] 
    }) 
    export class AppComponent { 
     title = 'ICS DashBoard Project'; 
     http: Http; 
     contacts = [ 
     'toto', 
     'momo', 
     'soso' 
     ]; 
     personne: Observable<personne[]>; 
     constructor(http: Http) { 
     //http.get('http://localhost:8082/personne/all'); 
     this.http = http; 
     this.http.get('http://localhost:8082/personne/all').subscribe((res: Response) => this.personne = res.json()); 


     } 

     addPersonne(nom: string, prenom: string, age: number, dateNaissance) { 


     /* 
     console.log(nom); 
      this.http.post('http://localhost:8086/personne/save', 
       {"prenom": prenom, 
       "nom":nom, 
       "age":age, 
       "dateNaissance":dateNaissance 
      }).toPromise(); * 
     */ 
     /* 
      var headers = new Headers({ 'Content-Type': 'application/json' }); 
      var options = new RequestOptions({ headers: headers }); 
      var body = JSON.stringify({ 
      var1:'test', 
      var2:'test2' 
      }); 
      var params='json'+body; 
      this.http.post('http://validate.jsontest.com', body, headers).map((res: Response) => res.json()); 
      console.log("ajoutee"); 
       */ 
     // var json=JSON.stringify({var1:'test1',var2:3}); 
     var params = { 

      "prenom": prenom, 
      "nom": nom, 
      "age": age, 
      "dateNaissance": dateNaissance 
     } 

     let options = new RequestOptions({ headers: headers }); 
     //var head = new Headers(); 
     console.log("TOTO"); 
     // head.append('Content-Type','application/x-www-form-urlencoded'); 
     /*this.http.post('http://localhost:8082/personne/save',params,{ 

      headers:head 
     }).map(res=> res.json())*/ 


     var headers = new Headers(); 
     headers.append('Content-Type', 'application/x-www-form-urlencoded'); 

     this.http.post('http://localhost:3001/sessions/create', params, { 
     headers: headers 
     }) 
     .map(res => res.json()) 
     .subscribe(


     () => console.log('Authentication Complete') 
     ); 
     } 
    /* 
    updatePersonne(oldName:string, newName:string){ 
     console.log("UPDATE"); 
     this.http.post('http://localhost:8082/personne/update', { "oldName": oldName, "newName": newName }); 
    }*/ 
      } 

這是我的休息控制器

  package org.sqli.Controller; 

     import java.util.List; 

     import org.sqli.entities.Personne; 
     import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.web.bind.annotation.CrossOrigin; 
     import org.springframework.web.bind.annotation.PathVariable; 
     import org.springframework.web.bind.annotation.RequestBody; 
     import org.springframework.web.bind.annotation.RequestMapping; 
     import org.springframework.web.bind.annotation.RequestMethod; 
     import org.springframework.web.bind.annotation.RestController; 
     import org.sqli.service.PersonneService; 

     @RestController 
     //@CrossOrigin("*") 
     //@CrossOrigin(origins = "http://localhost:4200") 
     @RequestMapping(value="/personne") 
     public class PersonneRestController { 

      @Autowired 
      private PersonneService personneService; 

      @CrossOrigin 
      @RequestMapping(value="/save") 
      public Personne savePersonne(@RequestBody Personne p){ 

       System.out.println("persoone :::"+p.getNom()); 
       return personneService.savePersonne(p); 

      } 
     /* @CrossOrigin 
      @RequestMapping(value="/update",method=RequestMethod.POST) 
      public Personne updatePersonne(@RequestBody String oldName , String newName){ 
       List<Personne> lPersonne = listPersonne(); 
       Personne newPersonne = null; 
       for(Personne personne : lPersonne){ 
        if(personne.getNom().equals(oldName)){ 
         newPersonne = personne; 
         newPersonne.setNom(newName); 
         break; 
        } 
       } 
        return personneService.updatePersonne(newPersonne); 

      }*/ 

     /* @CrossOrigin 
      @RequestMapping(value="savep",method=RequestMethod.PUT) 
      public Personne savePersonnep(@RequestBody Personne p){ 

       return personneService.savePersonne(p); 
      }*/ 
      @CrossOrigin 
      @RequestMapping(value="/all") 
      public List<Personne> listPersonne(){ 
       return personneService.listPersonne(); 
      } 
      @CrossOrigin 
      @RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE) 
      public void deletePersonne(@PathVariable Long id){ 
       System.out.println("Delete bien"); 
       personneService.deletePersonne(id); 

      } 




     } 
+0

有什麼結果的POST方法?錯誤?它是什麼?你做過什麼類型的調試?你發現了什麼? –

+0

我認爲內容類型是一個問題 –

+0

所以首先我開始調試,從我的其餘控制器的方法savePersonne它沒有執行,並且當我從瀏覽器檢查網絡時,get操作成功完成但它不是相同的情況對於後期的方法,我認爲問題是在應用程序components.ts – Amine

回答

0

你需要添加請求方法

@PostMapping(value = "/save", consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE }) 
public Personne savePersonne(@RequestBody Personne p){ 

       System.out.println("persoone :::"+p.getNom()); 
       return personneService.savePersonne(p); 

      } 

,並確認網址的服務(AppComponent/addpersonne)

+0

是的,現在後操作它成功完成,但我在調試瀏覽器中出現此錯誤 XMLHttpRequest無法加載http:// localhost:8082/personne/save。請求的資源上沒有「Access-Control-Allow-Origin」標題。因此不允許訪問Origin'http:// localhost:4200'。該響應具有HTTP狀態碼415. – Amine

+0

我試圖消除這個指令 headers.append('Content-Type','application/x-www-form-urlencoded'); 它現在的工作,非常感謝 – Amine

+0

爲你感到高興! – SuperGirl

0

使用控制器的終端不匹配的網址:

//addPersonne 
this.http.post('http://localhost:port/personne/save', params, { 
    headers: headers 
    }) 
    .map(res => res.json()) 
    .subscribe(


    () => console.log('Authentication Complete') 
    ); 
+0

後功能是的,我改變它,但偷了同樣的問題 – Amine