HttpErrorResponse 200 after successfull POST in Angular

有些话、适合烂在心里 提交于 2019-12-10 16:49:41

问题


I upload one file from Angular 5.xx to Jersey 1.xx via FormData.

The data gets received and saved successfully on my server app directory, but Browser says this line from the picture (Chrome and Firefox).

When I upload it via HTML only like so:

  Choose file to upload<br>
    <form action="http://localhost:8181/BackendMaven" method="post" enctype="multipart/form-data">
        <input name="input" id="filename" type="file" /><br><br>
        <button name="submit" type="submit">Upload</button>
    </form>

it won't show up.

Here is the angular FromData Code:

@ViewChild('fileInput') fileInput; 
submitFile(): void{ 
    console.log("submitFIle called!!!");
    let fi = this.fileInput.nativeElement;
     let fileToUpload = fi.files[0];
     let formData = new FormData();//empty formdata
     formData.append("input", fileToUpload);
     console.log(formData.get("input"));
     this.http.post(this.URI_UPLOAD,formData).subscribe();}

And Server Side (im using tomcat)

public Response uploadFile(@FormDataParam("input") InputStream uploadedInputStream, @FormDataParam("input") FormDataContentDisposition fileDetail) {
  ...
        return Response.status(200).entity("Successfully uploaded to location: " + FileFactory.getFilePath(uploadedFileLocation)).build();

    }

} 


回答1:


Try to actually send a JSON response, or tell angular to expect something different. If you're using HttpClient, it will by default expect JSON.

Something like this:

this.http.post(url, body, { responseType: 'text' }).subscribe();


来源:https://stackoverflow.com/questions/47378340/httperrorresponse-200-after-successfull-post-in-angular

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!