2017-03-09 94 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