How to POST(js/ajax) file to remote server

时间:2016-08-31 17:22:04

标签: javascript jquery ajax api

I try to send file to remote server by jQuery/AJAX. When I try use postman, it's ok, but when I try do in by jQuery, I get error:

XMLHttpRequest cannot load http://... . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access. The response had HTTP status code 400.

My code looks like(get it from postman):

var form = new FormData();
                form.append("file", {"0": item[0].files[0]});

                var settings = {
                  "async": true,
                  "crossDomain": true,
                  "url": url,
                  "method": "POST",
                  "headers": {
                  },
                  "processData": false,
                  "contentType": false,
                  "mimeType": "multipart/form-data",
                  "data": form
                }

                jQuery.ajax(settings).done(function (response) {
                  console.log(response);
                });

see logs here: http://pastebin.com/DvhTxVSw

So, how can I make an api request from client side, with jquery, to another server(where the web page is not hosted)?

0 个答案:

没有答案