Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajax throwing weird "Uncaught TypeError: Illegal invocation"

I have some jQuery code which is throwing a really weird error. Google Chrome calls the error Uncaught TypeError: Illegal invocation and says it is thrown in c.extend.param.e of jquery-1.4.4.min.js line 144, but backtraces it to my $.ajax call, which looks like this:

$.ajax({
   url: target,
   type: method,
   dataType: 'json',
   data: sendData,
   success: function(result) {
       if (result.redirect) {
           window.location = result.redirect;
       }
       else {
           for (var i in result) {
                if ($(i).size()) {
                    $(i).html(result.i);
                }
            }
        }
    }
});

Another question on SO which looks a bit like this attributes it to using $ without enclosing it in a jQuery function properly, but I'm pretty sure that's not my error this time, because I've been careful.

like image 573
Nathan MacInnes Avatar asked Nov 25 '10 17:11

Nathan MacInnes


2 Answers

Problems is here:

event.preventDefault();
var data = $.extend({
    referrer: window.location, <-- window.location is an object,
                                   should be window.location.href
    href: $(this).attr('href')
}, options.linkData);
loadPage(options.linkUrl, options.linkMethod, data);

Changing this makes it work, reason it breaks?

<jQUery1.4.4 at line 6079>
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value)

encodeURIComponent does not like the window.location object as it only takes strings.

See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

like image 131
Ivo Wetzel Avatar answered Oct 04 '22 18:10

Ivo Wetzel


try with jQuery.param with traditional parameter in true

documentation of param
modified

like image 37
andres descalzo Avatar answered Oct 04 '22 17:10

andres descalzo