ajax - jQuery cross-domain POST (file upload) -
this question has answer here:
- ways circumvent same-origin policy 11 answers
i'm making jquery requisition file upload:
<script> $('#form').submit(function(event) { event.preventdefault(); console.log("form[0]: \n"); console.log($('#form')[0]); var formdata = new formdata($('#form')[0]); $.ajax({ type: 'post', url: 'http://localhost:8080/project_url', data: formdata, cache: false, contenttype: false, processdata: false, beforesend: function() { $('#status').text('enviando...').attr('style','display: inline;'); }, complete: function(response, status) { console.log(status); $('#status').text('arquivo enviado com sucesso!').attr('style','display: inline;'); } }); }); </script>
i'm able send file server , page stays loading while file processed in server side. rest server returns json in following format: {"status" : "ok", "msg" : "response_message"}
. but, dont know how treat response , error:
xmlhttprequest cannot load http://localhost:8080/project_url. no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access.
i googled error , know happens because of cross-domain restrictions. need overcome restriction , json response (i need response know if file uploaded has right content)? it's possible that?
thanks!
edit:
server side method receive file:
@path("/import") @consumes(mediatype.application_json + ";charset=utf-8") @produces(mediatype.application_json + ";charset=utf-8") public class importresource { @post @path("/mapa") @consumes({mediatype.multipart_form_data}) public string uploadmapa( @formdataparam("file") inputstream fileinputstream, @formdataparam("file") formdatacontentdisposition filedetail, @formdataparam("colaborador") formdatabodypart jsonpart) { // here file processed... .... // return return response.ok("file ok");; } }
yes possible. need set header access-control-allow-origin: *
in rest service response data being returned form.
amend response {"status" : "ok", "msg" : "response_message"}
current response isn't in correct format.
i take end built on jersey
response response = response.status(200).entity(yourentity).header("access-control-allow-origin", "*").build();
you can restrict domain limited 1 server/client side/domain, please replace * domain restrict api to. e.g "www.yourdomain.com"
.
Comments
Post a Comment