【问题标题】:unable to upload image throuth Ajax and firefox无法通过 Ajax 和 firefox 上传图片
【发布时间】:2014-07-12 18:42:59
【问题描述】:

我想通过 Ajax 上传和图像到一个 php 脚本,但是我有这个来自 Firefox 的异常!!

发出请求时发生错误:[Exception...“'JavaScript 组件没有名为:“available”'的方法,调用方法时:[nsIInputStream::available]”nsresult:“0x80570030 (NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED)”位置:“JS 框架:::http://code.jquery.com/jquery-1.9.1.js::.send::8526 行”数据:无]

这里是我的 Ajax 代码:

"Save": function() {
    jQuery.ajax({
        url: ajaxurl,
            timeout:30000,
            type: "POST",
            data: {
                'action':'update_mini',
                'postdata': JSON.stringify(new FormData(document.getElementById("mini_form"))),//jQuery('#mini_form').serialize(),
                'block': jQuery.urlParam('block')
            },
            enctype: 'multipart/form-data',        
            cache:false,
            processData: false,
            contentType: false,                                
            error: function(XMLHttpRequest, textStatus, errorThrown)  {
                alert("An error has occurred making the request: " + errorThrown)
            },
            success: function(data) {
                alert(data);                                        
                //jQuery("#mDialog").hide();
                //jQuery( this ).dialog( "close" );
            }
    });

还有 HTML 表单代码:

<form id="mini_form" method="post">
    <fieldset>
        <label>Option Name :</label><br><input id="option" name="option" type="text"/>M<input id="m" name="m" class="minifield" type="text"/>P<input id="p" name="p" class="minifield" type="text"/><br>
 Image: <input type="file" name="image" id="image" />
    </fieldset>
</form>

谢谢。

【问题讨论】:

标签: javascript php jquery html ajax


【解决方案1】:

请通过将参数放入 formdata 来尝试这个。

if (window.FormData) {
    formdata = new FormData();

    finput = document.getElementById("fileupload-input");
    file = finput.files[0];

    if (file && !!file.type.match(/image.*/)) {
        if (formdata) {
          formdata.append("image", file);

          upSuccess = true;

          jQuery.ajax({
            url: ajaxurl,
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            async: false,
            success: function(e){
                if (e == 'error'){ //an error occurred in server side script
                    alert('An error occured while uploading the image.');
                    upSuccess = false;
                }
            }
          });

          if (upSuccess == false){
            return;
          }
        }

    } 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多