spring - is this ajax call for below rest Service is correct or not? -
sample rest service below:
@requestmapping(value = "/image/upload", method = requestmethod.post) public void uploadimage(@requestparam("image") multipartfile fileobj) throws exception { system.out.print("file name:"+fileobj.getoriginalfilename()); }
and wrote ajax code : , accept application format json when call 400 error
$('#user_click').click(function(){ var data = { image:$("#file_1").val }; $.ajax({ url : "http://localhost:8080/myproject/image/upload", type : "post", contenttype : false, crossdomain : true, data : json.stringify(data), datatype : 'json', async : true, success : function(result) { alert('the selected items uploaded'); }, error: function(message){ alert("error:"+json.stringify(message)); } });
is ajax code correct or not?
no, not work since ajax request not transfer file data.
the solutions
- use file upload plugin jquery-form
ex:
$('#myform').ajaxsubmit({ url : 'http://localhost:8080/myproject/image/upload', type : "post", datatype : "json", success : function(response) { }, error : function(response) { } });
- use html5 formdata (sadly no ie support)
Comments
Post a Comment