gpt4 book ai didi

javascript - 如何添加ajax参数?

转载 作者:行者123 更新时间:2023-12-03 04:26:26 25 4
gpt4 key购买 nike

面临这样一个问题,就是无法给ajax请求添加参数,因为有一个“Form Data”,添加更多选项是不行的。当您在参数“data”中添加另一个变量时,就会发生错误。如何做到这一点是在一个请求中发布一个文件和参数?

请原谅这个错误,无论错误与否,php 文件根本不输出任何内容,而是空值

//Here the file is sent without problems, but the parameters no more don't accept php file, nothing displays in the result
var fd = new FormData();
fd.append('file', input[0].files[0]);
$.ajax({
url: "/controllers/createNewsController.php",
data: fd,
type: "POST",
processData: false,
contentType: false,
success: function(data) {
$(".news .containers").append(data);
}
});

//When this code runs the PHP file won't take anything, although I was expecting the output variables and the file. Using var_dump, $_POST and $_FILES displays array(0){}
var fd = new FormData();
fd.append('file', input[0].files[0]);
$.ajax({
url: "/controllers/createNewsController.php",
data: {fd, title:newsHeader, description:description, hashTag:hashTag, themeHashTag:themeHashTag, viewNews:viewNews},
type: "POST",
processData: false,
contentType: false,
success: function(data){
$(".news .containers").append(data);
}
});

//Similarly, nothing appears in the php file
var fd = new FormData();
fd.append('file', input[0].files[0]);
$.ajax({
url: "/controllers/createNewsController.php",
data: {fd:fd, title:newsHeader, description:description, hashTag:hashTag, themeHashTag:themeHashTag, viewNews:viewNews},
type: "POST",
processData: false,
contentType: false,
success: function(data){
$(".news .containers").append(data);
}
});

最佳答案

就像附加文件一样,您可以向其中附加更多数据,如下所示:

fd.append('file', input[0].files[0]);

fd.append('var1', val1);
fd.append('var2', val2);

关于javascript - 如何添加ajax参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43695737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com