使用以下功能时出现以下错误:
无法加载资源:服务器响应状态为414(Request-URI太大)。
当数据不太长时,该函数工作正常,问题是,在某些情况下,rez和com变得冗长,而ajax似乎使用GET,而不是POST。 任何人都可以向我解释为什么ajax在我宣布POST时使用GET?我怎么能防止这种情况?谢谢。
jQuery('#buton_submit').click(function() {
var rez = {};
var com = {};
jQuery("input[name^='sb-rez']:checked").each(function() {
rez[jQuery(this).attr('name').substr(7)] = jQuery(this).val();
});
jQuery("textarea[name^='sb-com']").each(function() {
if (jQuery(this).val().length > 0) {
com[jQuery(this).attr('name').substr(7)] = jQuery(this).val();
}
});
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
data:{
'action':'admin_event',
'fn':'saveResultsEvent',
'method': 'POST',
'contentType': 'application/x-www-form-urlencoded;charset=UTF-8',
'processData': false,
'r': rez,
'c': com,
'pid': jQuery('div[id="content"] :first-child').attr('id').substr(5)
},
success: function(data){
jQuery('#arata_mesaje').html(data);
jQuery('#arata_mesaje').show('fast');
jQuery('#arata_mesaje').delay(3000).hide(1000);
},
error: function(errorThrown){
alert('Error printed in console.');
console.log(errorThrown);
}
});
});
更正.ajax电话,以备将来参考(试图将其作为答案发布,但由于缺乏声誉,我无法再回答我自己的问题8小时):
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
processData: false,
data:{
'action':'admin_event',
'fn':'saveResultsEvent',
'r': rez,
'c': com,
'pid': jQuery('div[id="content"] :first-child').attr('id').substr(5)
},
success: function(data){
jQuery('#arata_mesaje').html(data);
jQuery('#arata_mesaje').show('fast');
jQuery('#arata_mesaje').delay(3000).hide(1000);
},
error: function(errorThrown){
alert('Error printed in console.');
console.log(errorThrown);
}
});
问题是我错误地在数据数组中声明了类型/方法和contentType,而不是作为ajax调用的参数,默认情况下该方法是GET。
答案 0 :(得分:0)
您输错了 - 查看jQuery.ajax()
here的文档,您会看到您定义了type
密钥使用的方法,而不是method
。所以你的请求应该是这样的:
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
data:{
'action':'admin_event',
'fn':'saveResultsEvent',
'type': 'POST',
'contentType': 'application/x-www-form-urlencoded;charset=UTF-8',
'processData': false,
'r': rez,
'c': com,
'pid': jQuery('div[id="content"] :first-child').attr('id').substr(5)
},
success: function(data){
jQuery('#arata_mesaje').html(data);
jQuery('#arata_mesaje').show('fast');
jQuery('#arata_mesaje').delay(3000).hide(1000);
},
error: function(errorThrown){
alert('Error printed in console.');
console.log(errorThrown);
}
});