我为 ajax 请求定制了一个自定义限制。
问题是我不断收到此错误?
<小时/>未捕获类型错误:无法在“窗口”上执行“postMessage”:需要 1 个参数,但仅存在 0 个。
<小时/>该行指向 $.ajax({
.
HTML:
<input class="image_title" />
<span class="the_title"></span>
JS:
$(function() {
var aj_count = 0;
var aj_flag = false;
var aj_flag2 = false;
var run_on = -1;
setInterval(function() {
aj_count++;
if (aj_flag === true) {
run_on = aj_count + 250;
aj_flag = false;
aj_flag2 = true;
}
if (run_on < aj_count && aj_flag2 === true) {
var $t = $(this);
var daid = $('.image_id').val();
aj_flag2 = false;
$.ajax({
type: "POST",
url: '/ajax/set_title.php',
data: {
'title' : $t,
'id' : daid
},
success: function(data) {
var data = JSON.parse(data);
$('.the_title').html( '<small>Title:</small> <strong>' + data.title + '</strong>' );
}
});
}
}, 1);
$('.image_title').on('input', function(e) {
aj_flag = true;
e.preventDefault();
return false;
});
$('.image_title').on('keydown', function(e) {
if (e.which == 13) {
e.preventDefault();
return false;
}
});
});
正如你所看到的,我尝试将直接形式的值转移到变量等中,但我无法再让它工作了。当我用 console.log 替换 ajax 部分时,它会按预期运行。我一直在环顾四周,但我并不真正理解错误的含义,因为 ajax 传递了一个数组。
感谢您的宝贵时间
最佳答案
该错误可能是由于
var $t = $(this);
您正尝试将 $t
作为 title:
参数的值发送
data: {
title: $t,
id: daid
},
但是 jQuery 对象无法序列化为 POST 参数。
您需要将 $t
设置为正确的标题字符串。我不知道它在您的应用程序中的什么位置,但这应该可以解决它。
关于javascript - 无法在 Ajax 上的窗口上执行 postMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60768911/