anon
anon

Reputation:

Failed to execute postMessage on window on Ajax

I've made a custom throttle for ajax requests.

Problem is I keep getting this error?


Uncaught TypeError: Failed to execute 'postMessage' on 'Window': 1 argument required, but only 0 present.


The line points to $.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

As you can see I have tried moving direct form vals into variables etc but I cannot get it to work anymore. When I replace the ajax section with a console.log it runs as expected. I've been looking around but I don't really understand what the error means still as ajax has an array passed to it.

Thank you for your time

Upvotes: 0

Views: 527

Answers (1)

Barmar
Barmar

Reputation: 781235

The error is probably because of

        var $t = $(this);

You're trying to send $t as the value of the title: parameter with

data: {
    title: $t,
    id: daid
},

But a jQuery object can't be serialized into a POST parameter.

You need to set $t to a proper title string. I don't know where that is in your application, but that should fix it.

Upvotes: 1

Related Questions