2010-11-25 14 views
13

Tengo un código jQuery que arroja un error realmente extraño. Google Chrome llama el error Uncaught TypeError: Illegal invocation y dice que es lanzada en c.extend.param.e de la línea jquery-1.4.4.min.js 144, pero trazas inversas a mi llamada $ .ajax, que se ve así:

$.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); 
       } 
      } 
     } 
    } 
}); 

Otra pregunta en SO que se parece un poco a esto lo atribuye al uso de $ sin encerrarlo en una función jQuery correctamente, pero estoy bastante seguro de que no es mi error esta vez, porque he tenido cuidado.

Respuesta

20

problemas está aquí:

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); 

El cambio de este hace que funcione, por lo que se rompe?

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

encodeURIComponent no le gusta el objeto window.location ya que sólo tiene strings.

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

+0

Gracias, eso se arregló. –

+4

No hay problema :) Tomó solo 5 minutos (bueno, si sabe lo que necesita buscar) –

+0

¿Dónde event.preventDefault(); datos var = $ .extend ({ de referencia: window.location, <- window.location es un objeto, debería haber window.location.href href: $ (este) .attr ('href') } , options.linkData); loadPage (options.linkUrl, options.linkMethod, data); vienen? El asker no incluyó ese código en esto en su pregunta ... –

2

tratan con jQuery.param con el parámetro tradicional en cierto

documentation of param
modified

+0

Gracias, ese fue otro problema. No fue el que arrojó el error, pero me has ahorrado muchos arañazos en la cabeza. –

Cuestiones relacionadas