簡體   English   中英

使用http put第0:-1行將值從Angular 5傳遞到Java REST API,在輸入&#39; <EOF>

[英]Pass value from Angular 5 to Java REST API using http put, line 0:-1 no viable alternative at input '<EOF>

我遇到了一個問題,嘗試學習整個Web跟蹤數據的周期,我將值從Java jesay REST-API傳遞給了angular 5,但是我想將文本從angular傳遞給后端以執行一些查詢並再次返回結果,我對@FormParam和許多東西感到困惑,

下面是我的代碼,

File-list.service.ts

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { FileList } from './file-list';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map'
save(fileList: FileList): Observable<Response> {

console.log('Saving person ' + JSON.stringify(fileList));
return this.http.put('http://localhost:8080/SWBackend/jaxrs/Person/Save', JSON.stringify(fileList), { headers: this.getHeaders()});}

List.component.html

<form (click)="save()" method="POST" action="http://localhost:8080/SWBackend/jaxrs/Person/Save">
    <p>
        Path : <input type="text" name="pathUrl" />
    </p>
    Press to show the files
    <input type="submit" value="Add User" />
    <input type="hidden" name="_method" value="PUT">
</form>

Java文件.java

@PUT
@Path("Save")
@Consumes({ "application/xml", "application/json" })
@Produces("application/json")
public Response saveEmp(FilesList pathUrl) {
    System.out.println("Reach Save Emp");
    return Response.ok().header("Access-Control-Allow-Origin", "*").build();
}

我收到此錯誤:

行0:-1在輸入''處沒有可行的選擇

我不知道我的方法是對還是錯,但是自從一周前我一直在嘗試,

謝謝你們。

試試這個,

在您的表單中刪除以下內容:

<form (click)="save()" method="POST" action="http://localhost:8080/SWBackend/jaxrs/Person/Save">

使用FormGroup和FormControl為表單使用ReactiveFormsModule。

然后,在您的組件中,提交表單時,它將調用該函數來調用您的服務,該服務將與您的Java控制器進行通信。

該組件應具有與您的template(html)中的formGroup關聯的功能。

然后,該函數應調用您的服務,例如:this.service.save(fileList);

https://angular.io/guide/reactive-forms

例如:

<form [formGroup]="fileListForm" (ngSubmit)="save(fileListForm.value)">
    <input type="text" formControlName="fileListNameControl">
</form>

零件:

private fileListForm: FormGroup;
private fileListNameControl: FormControl;

 ngOninit(){
   this.fileListForm = new FormGroup({
    fileListNameControl: new FormControl("", Validators.required)
   });
 }



save(form: any){
 // get your form values here and then call service
  this.fileLIstService.save('your value');
}

**確保通過模塊中的@ angular / forms導入ReactiveFormsModule。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM