我想用ajax上傳包含數據的文件。 這裏是我的HTML表單:用ajax發送上傳的文件和輸入數據
<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);
}
});
但是。我不知道如何與數據
var DATA = 'name='+name;
$.ajax({
url: "test.php",
type: "post",
data: DATA,
success: function (response) {
console.log($response);
},
});
感謝
只需一個ajax,你可以通過使用'form_data.append('input_name',$('#name')。val());' –