使用AJAX和远程PHP上传文件

时间:2016-08-01 13:15:08

标签: javascript php jquery ajax xmlhttprequest

我正在尝试使用AJAx构建一个上传器。我想使用AJAX而不是表单和iframe,因为我希望默认的CSS样式与响应一起使用。

问题是,我的PHP在远程服务器上。我正在使用Shopify,他们不允许我将他们的服务器用于PHP,所以我在不同的服务器上使用它。出于某种原因,它不起作用。这是我的代码:

<form action="" id="formContent" method="post" enctype="multipart/form-data" >
<input  type="file" name="file"  required id="upload">
<button class="submitI" >Upload Image</button> 
</form>


<script type="text/javascript">
$("#formContent").submit(function(e){
    e.preventDefault();

var formdata = new FormData(this);

    $.ajax({
      url: "http://myserver.com/upload.php",
        type: "POST",
        data: formdata,
        mimeTypes:"multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        crossDomain: true,
        success: function(){
            alert("file successfully submitted");
        },
        error: function(){
            alert("okey");
        }
     });
  });

我所得到的只是错误信息。 我按照此回复中列出的代码:https://stackoverflow.com/a/38450277/6442152

我也尝试过使用xmlhttprequest:

$(':file').change(function(){
var file = this.files[0];
name = file.name;
size = file.size;
type = file.type;

if(file.name.length < 1) {
}
else if(file.size > 100000) {
    alert("The file is too big");
}
else { 
    $(':submit').click(function(){
        var formData = new FormData($('*formId*')[0]);
        $.ajax({
            url: 'script',  //server script to process data
            type: 'POST',
            xhr: function() {  // custom xhr
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ // if upload property exists
                    myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // progressbar
                }
                return myXhr;
            },
            // Ajax events
            success: completeHandler = function(data) {
                /*
                * Workaround for Chrome browser // Delete the fake path
                */
                if(navigator.userAgent.indexOf('Chrome')) {
                    var catchFile = $(":file").val().replace(/C:\\fakepath\\/i, '');
                }
                else {
                    var catchFile = $(":file").val();
                }
                var writeFile = $(":file");
                writeFile.html(writer(catchFile));
                $("*setIdOfImageInHiddenInput*").val(data.logo_id);
            },
            error: errorHandler = function() {
                alert("Something went wrong!");
            },
            // Form data
            data: formData,
            // Options to tell jQuery not to process data or worry about the content-type
            cache: false,
            crossDomain: true,
            contentType: false,
            processData: false
        }, 'json');
    });
}});

这也让我收到错误消息然后它说404文件未找到

我也将此标题添加到我的PHP中,但它不起作用:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');

0 个答案:

没有答案