Pregunta

Me acaba de entrar en el mundo de jQuery y bastante nuevo en javascript también.yo tener un pequeño fragmento de código JavaScript, como a continuación: -

<script type="text/javascript">
 $(function(){
    $('a').click(function(event){
        event.preventDefault();
        $.get('/_add_navigation_',function(response){
        $('#themaincontents').html(response);
        })
   })
</script>

Las miradas del HTML, como a continuación: -

 <a href="?toaddnavigation">CLICK Me</a>
 <div id="themaincontents"></div>

En el lado del servidor que hago una comprobación de cabecera xhr por algo así como

 if request.is_xhr: send response else:redirect somewhere

Ahora bien, aunque este código funciona bien en Chrome y Opera, Firefox en que se comporta un poco raro. El servidor no envía de vuelta al lugar Reponse hace una redirección. Eso significa que se dice que no hay ningún encabezado xhr. ¿Por qué debería suceder esto, mientras que en los otros dos navegadores que está funcionando bien. (Estoy usando Firefox 3.6.12) Update-Sólo tenía un vistazo a las cabeceras de petición de Firefox y no encuentro X-solicitada-Con: cabecera XMLHttpRequest, pero está presente en cromo)

¿Fue útil?

Solución

Not all browsers send the same headers, and you cannot rely on them to be consistent across browsers. The easiest way is to not rely on the browser to send something, but manually send something yourself:

$.get('url', {
    xhr: 'yes' // add this extra parameter here
}, function(){

});

then check for that GET variable on the server instead of a header that may or may not be sent by a browser.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top