jQuery,将$ .ajax方法转换为$ .post [英] jQuery, Convert $.ajax method to $.post
问题描述
我正在尝试制作一个简单的表单以使用jquery/ajax上传文件.这是我的代码的一部分:
i'm trying to make a simple form to upload files with jquery/ajax. this is a part of my code:
var formData = new FormData($(this)[0]);
$.ajax({
url: 'uploader.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
$('#ShowR').html(data);
}
});
我正在尝试将此代码更改为$ .post方法,如下所示:
i'm trying to change this code to $.post method like this:
var formData = new FormData($(this)[0]);
$.post('uploader.php', {action:"ShowGTR",MyFiles:formData},
function(data) {
$('#ShowR').html(data);
});
我尝试了一些方法,但是我无法修复代码,在Google Chrome控制台中,出现了此错误:
i tried some ways but i couldn't fix the code and in Google Chrome console i got this error:
Uncaught TypeError: Illegal invocation
所以我需要您的帮助来修复此代码,并将<$.ajax方法转换为$ .post方法.我真的很感激任何人都可以帮助我.
so i need your help to fix this code and convert $.ajax method to $.post method. i really appreciate if anyone can help me for this.
推荐答案
如果要将$ .ajax调用转换为$ .post调用的唯一原因是添加参数,则无需转换它ro $ .post.您需要做的是将参数附加到formdata对象
If the only reason you want to convert your $.ajax call to a $.post call is to add a parameter, then you don't need to convert it ro $.post. What you have to do is append the parameter to the formdata object.
var formData = new FormData(this);
formData.append("action", "ShowGTR");
$.ajax({
url: 'uploader.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
$('#ShowR').html(data);
}
});
这篇关于jQuery,将$ .ajax方法转换为$ .post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!