Jquery和AJAX发布到php数据属性?

时间:2016-11-27 18:38:24

标签: javascript php jquery ajax

您好我有以下AJAX代码:

 var formData = new FormData($('form')[0]);
$.ajax({
    url: 'saveImage.php',  //Server script to process data
    type: 'POST',
    data: formData,
    processData: false,
    success: function(data){
        console.log(data);            
    }

});

它运行良好,它加载了PHP页面,它应该是背景:

    <?php
        include_once "mysql_connect.php";
        $imageName = mysql_real_escape_string($_FILES["Image1"]["name"]);
        $imageData = '';
        $imageext = '';
        if($imageName != null){
            $imageData = mysql_real_escape_string(file_get_contents($_FILES["Image1"]["tmp_name"]));
            $imageType = mysql_real_escape_string($_FILES["Image1"]["type"]);
            $imageSize = getimagesize($_FILES["Image1"]["tmp_name"]);
            $imageType = mysql_real_escape_string($_FILES["Image1"]["type"]);
            $FileSize = FileSize($_FILES["Image1"]["tmp_name"]);
            $imageext = mysql_real_escape_string($imageSize['mime']);
        }
        $query=mysql_query("INSERT INTO pictures (`id`, `imagedata`, `imageext`) VALUES ('', '$imageData', '$imageext');"); 


        echo $imageext;


?>

唯一的问题是PHP页面无法找到变量Image1,它是表单中输入的名称。我做错了什么吗我在想,也许在Ajax中的数据参数中它会是这样但是正确的:

data: "Image1"=formData,

这是一件事,如果不是为什么我的PHP看不到输入字段?

1 个答案:

答案 0 :(得分:0)

您忘记了Ajax函数中的cache和contentType属性。试试它应该工作:

var formData = new FormData($('form')[0]);
$.ajax({
   type: "POST",                
   url: "saveImage.php",
   processData: false,
   contentType: false,
   cache:false,
   data: formData,
   success: function(data){
        console.log(data);
    }
});