javascript - 无法在 Ajax 上的窗口上执行 postMessage

标签 javascript ajax

我为 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;
    }
});

});

enter image description here

正如你所看到的,我尝试将直接形式的值转移到变量等中,但我无法再让它工作了。当我用 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/

相关文章:

javascript - React Native,onPress 不适用于从 fontAwesome 导入的图标

javascript - asbygoogle 导致 chrome 执行脚本永远不会返回

javascript - 传递键值数组 AJAX

php - 如何在 React 应用程序中包含 PHP 脚本

javascript - rails/jquery/ajax : done callback is not executing

javascript - 在 JavaScript 中查找与字符串不匹配的行

javascript - 当以编程方式更改输入时,更改的解决方案不起作用

javascript - jQuery ajax 引用网址

jquery - 将嵌套的复杂 json 传递给 MVC Controller

php - 如何使用 PHP 跟踪数据库表中的更新?