简体   繁体   中英

Couldn't catch the response through Angular file upload

I am trying to upload a single file using POST Method and REST Calling to the server. Using this following code, I am able to upload the module but It shows me some error after those uploading. Can someone help me to solve this problem?

My HTML Code :

  <form name="myForm" enctype="multipart/form-data" >
      <input type="file" ngf-select ng-model="picFile" name="file" file="file"     
             accept="" ngf-max-size="2MB" required
             ngf-model-invalid="errorFile">
      <button ng-disabled="!myForm.$valid" 
              ng-click="uploadPic(picFile)">Submit</button>
  </form>

MY JS Code :

var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload','fileUpload', function ($scope, Upload,fileUpload) {
    $scope.uploadPic = function(file) {
      fileUpload.uploadFileToUrl(file);
    }
}]);

app.service('fileUpload', ['$http','Upload', function ($http,Upload) {
    this.uploadFileToUrl = function(file){
    file.upload = Upload.upload({
      url: 'http://localhost:8080/openmrs/ws/rest/v1/module/?',
      headers: {'Content-Type': 'multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p'},
      transformRequest: angular.identity,
      data: {file: file}
    });
     file.upload.then(function (response) {
            }, function (response) {
                if (response.status > 0)
            }, function (evt) {
            });
    }
}]);

Error Message :

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: The classloader is null Module: openhmis.inventory (through reference chain: org.openmrs.module.Module["moduleActivator"]); nested exception is org.codehaus.jackson.map.JsonMappingException: The classloader is null Module:

The error message showed like "Could not write JSON". That means It could not write the JSON type response to the request. Please check with your Response type and confirm Is it JSON type.

There are a lot of response types, that are

  1. text/html
  2. application/xhtml+xml
  3. application/xml;q=0.9
  4. image/webp, / ;q=0.8

Better change your header with Accept status like this following,

  headers: {'Content-Type': undefined ,  'Accept': 'application/xhtml+xml;charset=UTF-8'}

It is working ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM