Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:[]。文件未定义。在jquery文件post中_Javascript_Jquery - Fatal编程技术网

Javascript TypeError:[]。文件未定义。在jquery文件post中

Javascript TypeError:[]。文件未定义。在jquery文件post中,javascript,jquery,Javascript,Jquery,当我尝试使用jquerypost上传图像文件时。 下面给出了我对这篇文章的jquery $(document).ready(function(){ $('#submit_img').bind("click",function(){ if(validateProfile()){ //alert(img); var img = new FormData(); img.append('file',[].f

当我尝试使用jquerypost上传图像文件时。 下面给出了我对这篇文章的jquery

$(document).ready(function(){

    $('#submit_img').bind("click",function(){
        if(validateProfile()){
            //alert(img);
            var img = new FormData();
            img.append('file',[].files[0]);
            //$('#file_upload')[0].files[0];
        $.post(profileUrl,{'api_key':apiKey,
                 'api_secret':apiSecret,
                 'id_user': userid,
                 'profile_image':img,
                 },function(data){
                     $.each($.parseJSON(data),function(key,value){

                         var stStatus = value.status; 
                         var stText = value.status_text;
                         alert(stText);
                         //$("#error_username").html(stText);    

                    });     
           });
        }
    });
});

当按下提交按钮时,给出错误lke“TypeError:[].files is undefined”我不知道确切原因是什么。我不熟悉jquery。

第6行,append函数的第二个参数,似乎您无意中删除了获取输入元素的引用


因此JavaScript将[]解释为创建一个新数组,然后尝试访问其中的文件。

尝试将[].files[0]替换为$(“#file”)[0]。files[0],其中#file中的file是您的输入元素id

类似于“NS\U error\U XPC\U BAD\U OP\U ON\U WN\U PROTO:WrappedNative prototype对象上的非法操作。jquery-1.4.1.min.js:130”的错误是。当时我使用的是“var fd=new FormData();fd.append('file',input.files[0]);”