如何使用GrailsWebService通过jQueryAjax上传图像

如何使用GrailsWebService通过jQueryAjax上传图像,jquery,ajax,grails,Jquery,Ajax,Grails,我有一个Web服务来上传通过GRAILS实现的文件。 我可以使用以下命令使用CURL使用web服务: curl -X POST -H "Authorization: 53676c55f8014c5eb84624b49d9c9e74" -H "Content-Type: multipart/form-data" http://localhost:8080/project/api/profiles/11/uploadlogo -F "img=@c:/my_image.jpg" 如何使用jquery

我有一个Web服务来上传通过GRAILS实现的文件。 我可以使用以下命令使用CURL使用web服务:

curl -X POST -H "Authorization: 53676c55f8014c5eb84624b49d9c9e74" -H "Content-Type: multipart/form-data" http://localhost:8080/project/api/profiles/11/uploadlogo -F "img=@c:/my_image.jpg"
如何使用jqueryajax函数使用它

我确实喜欢这样,但它不起作用:

var files = [];

$('input').change(function(e){            
    files = e.target.files;    
});

$('form').on('submit', function(e){

  e.stopPropagation();
  e.preventDefault();

  var data = new FormData();
  $.each(files, function(i, file) {
     console.log(file)
     data.append('img', $('input').val());
  });

  $.ajax({
    url: 'http://localhost:8080/hsp-main/api/profiles/11/uploadimage',
    contentType: 'false',
    type: 'post',
    processData: false,
    header: {
      "Authorization": '88db1ec45c3f4882859d59df45900fd5'
    },
    data: data
    }).done(function(response){
      console.log(response);
    }).fail(function(a, s, d){
      console.log(a, s, d);
    });

     return false;
});
这就是解决办法

            var data = new FormData();
            data.append("img", $('input[type="file"]')[0].files[0]);

            $.ajax({
                url: 'http://64.15.146.86:9090/hsp-main-0.1/api/profiles/11/uploadimage',
                cache:false,
                processData:false,
                contentType:false,
                type:'POST',
                success:function (data, status, req) {
                    console.log(req);
                },
                error:function (req, status, error) {
                    console.log(req);
                },
                headers: {
                    'Authorization': 'ec5ce080a64941c68a84cac8f28293dd'
                },
                data: data
            }).done(function(response){
                console.log(response);
            }).fail(function(a, s, d){
                console.log(a, s, d);
            });