使用ajax发送上传的文件和输入值

时间:2017-05-11 20:33:09

标签: javascript html ajax

我正在尝试使用ajax上传带有数据的文件。 这是我的html表单:

<body>
  <input type="text" id="name" value="test" />
  <input type="file" id="pic" accept="image/*" />
  <input id = "submit" type="submit" />
</body>

当我单独使用ajax发送上传的文件时,它正在使用新的FormData();

var file_data = $('#pic').prop('files');   
var form_data = new FormData();                  
form_data.append('file', file_data);
alert(form_data);                             
$.ajax({
            url: 'test.php', // point to server-side PHP script 
            dataType: 'text',  // what to expect back from the PHP script, if anything
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,                         
            type: 'post',
            success: function(php_script_response){
                alert(php_script_response); 
            }
 });

但我不知道如何使用Data

发送输入'name'
var DATA = 'name='+name;

$.ajax({
  url: "test.php",
  type: "post",
  data: DATA,
  success: function (response) {
    console.log($response);             
  },
});

由于

0 个答案:

没有答案