如何在post数据中进行ajax调用-->图像和自定义数据(名称和值对)
How to make ajax call in post data --> image & custom data(name and value pair)
>我正在创建一个应用程序来上传图像及其路径,并且每个图像都有单独的路径
data: {name:"niks",age:"22",path:"small/data",image:new FormData(this)}
我也想接收PHP代码中的数据和图像。
逻辑上我已经写了这段代码,什么是正确的代码请帮帮我。
HTML 表单代码是
<form id="replaceimg" enctype="multipart/form-data" method="post">
<input name="fileimg" type="file" id="fileimg" required />
<input type="submit" value="Upload Image" name="submit" />
</form>
我的代码是:
$("#replaceimg").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: {name:"niks",age:"22",path:"small/data",image:new FormData(this)}, // Data sent to server, a set of key/value pairs representing form fields and values
contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
success: function(data) // A function to be called if request succeeds
{
console.log("data is "+data);
}
});
alert("In Submit replaceimg");
}));
您必须将 FormData 对象作为数据参数传递。 jQuery AJAX 'multipart/form-data' 不发送数据?
但是,或者,如果图像不是那么大,您可以上传base64字符串。
试试这个:
var uploadFile = $('.nameOfInputClass') // <- Change the class.
var data = {};
data.name = 'Niks';
data.age = 22; // Should be a Number, not a string.
data.path = 'small/data';
data.image = new FormData();
data['image'].append('upload', uploadFile[0]);
他们:
$("#replaceimg").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: data,
contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
success: function(data) // A function to be called if request succeeds
{
console.log("data is "+data);
}
});
alert("In Submit replaceimg");
}));
相关文章:
- jquery数据表的自定义ko绑定
- 自定义验证并格式化从数据数据库返回的响应-BEGINNER
- 如何从自定义对话框编辑数据网格中的选定行
- 使用自定义数据属性或将数据绑定到处理程序来处理事件
- 选项中包含数据的自定义标记
- ListView的ItemTemplate内的自定义HtmlControl的数据绑定失败
- MVC中关于表的自定义工具提示-每行显示数据
- 数据表自定义筛选
- 如何正确地将数据从自定义服务传递到角度控制器
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- 从sql填充Jqgrid的自定义Add函数中的下拉数据
- 使用水豚,i'm无法从href中读取自定义数据-属性
- 自定义筛选器不'我不使用django数据表视图创建的ajax.data.Json
- 在每次文件上传时发送带有放置区的自定义数据.js
- AngularJS -- 创建自定义数据绑定和 ng-repeat
- 如何将从谷歌自定义搜索API获取的数据放入复选框列表
- 如何自定义数据::confirm弹出窗口以提示输入,该输入将附加到请求参数中
- 将数据传递到自定义控制台
- 试图使一个事件依赖于另一个事件,并使用jquery自定义事件传递数据
- jquery数据没有解析值html5数据自定义属性