使用 jquery ajax 上传文件的数据方法设置
Data method settings for file upload using jquery ajax
所以这个问题源于在使用 ajax 时缺乏对数据方法如何运作的良好理解。我已经在这里查看了有关ajax的大多数其他问题,但这部分不适合我。
例如,我使用 ajax 将表单数据(包括多个文本字段和一个文件上传)发送到 php。然后,服务器脚本在 sql 数据库中插入字段数据和文件 url,然后将所有这些值作为 json 编码数组返回。
我当前的ajax脚本如下所示,当它运行时,除了文件上传部分外,一切正常。文件没有保存到服务器,当然也没有url回来。 如果我更改:
data: data,
自
data: text,
文件上传有效,文件被保存,但是当 JSON 数组返回到页面时,我会看到一个显示数组数据的白屏(它不会返回到表单数据来自的页面)。
这是为什么呢?使用什么正确的数据方法来允许我的文件上传工作并使数据返回到发送它的页面。
$("document").ready(function() {
$(".data-form").submit(function() {
var data = new FormData();
$.each(files, function(key, value)
{ data.append(key, value);
});
if (confirm("'t't'tAre you ready to sumbmit this listing?'nYou can always edit the listing after going to the menu tab - edit listing.")) {
$.ajax({
type: "POST",
dataType: "json",
url: "add-list.php",
data: data,
processData: false,
contentType: false,
success: function(response) {
if (response.success) {
$("#modal1").modal('hide');
$("#add_frame").show();
$("#newbutt").show();
$(".atitle").html('<a href="' + response.ad_linka + '" target="_blank">' + response.titlea + '</a>');
$(".acomment").html(response.commenta);
$(".aaddress").html(response.addressa);
$(".asale-price").html(response.sale_pricea);
$(".alease-price").html(response.lease_pricea);
$(".alot-size").html(response.lot_sizea);
$(".abuilding-size").html(response.build_sizea);
$(".azoning").html(response.zoninga);
}
else {
console.log("An error has ocurred: sentence: " + response.sentence + "error: " + response.error);
}
},
error: function() {
alert("An Error has ocurred contacting the server. Please contact your system administrator");
}
});
return false;
}
});
});
你需要告诉jquery停止处理你的数据
processData: false, // Don't process the files
contentType: false, // defaults to 'application/x-www-form-urlencoded; charset=UTF-8'
您没有发送构建数据的方式,但这样的事情应该可以工作
var data = new FormData();
$.each(files, function(key, value)
{ data.append(key, value);
});
相关文章:
- 序列化数据属性中对象的最可靠方法
- 有没有一种方法可以仅使用HTML/CSS来隐藏基于特定值的数据单元格
- 如何在从客户端接收数据时从本机方法触发javascript函数?
- 我们可以用参数对象集合而不是原始数据来调用JavaScript collection.reduce()方法吗
- 在put方法之前从作用域获取数据
- 使用ReactJS和Flux架构从服务器获取数据的正确方法是什么
- 有没有一种方法可以使用Javascript检索谷歌表单ITSELF(而不是电子表格)的数据
- 有没有一种简单的方法可以用Lodash映射嵌套数据
- 数据属性有没有更干净的方法
- 将数据从promise then方法传递到对象方法
- 有没有更好的方法可以测量从Ajax调用返回数据所需的总时间
- ExtJS :: 存储加载数据方法优化
- 如何通过javascript触发rail的数据方法=“删除”
- 使用 jquery ajax 上传文件的数据方法设置
- 使用 d3 js 库的数据方法将事件绑定到动态生成的内容
- 插件内部jquery数据方法的奇怪行为
- 访问自定义组件数据/方法
- 为什么数据方法在获取ID时不能正常工作
- 为什么jQuery'数据方法的行为是这样的?(可能是错误?)
- jQuery数据方法返回整数而不是字符串