Pasar encabezados de solicitud en una llamada jQuery AJAX GET
Estoy tratando de pasar encabezados de solicitud en un AJAX GET usando jQuery. En el siguiente bloque, "data" pasa automáticamente los valores en la cadena de consultas. ¿Hay alguna manera de pasar esos datos en el encabezado de la solicitud en su lugar ?
$.ajax({
url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
data: { signature: authHeader },
type: "GET",
success: function() { alert('Success!' + authHeader); }
});
Lo siguiente tampoco funcionó
$.ajax({
url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
beforeSend: { signature: authHeader },
async: false,
type: "GET",
success: function() { alert('Success!' + authHeader); }
});
3 answers
Use beforeSend
:
$.ajax({
url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
data: { signature: authHeader },
type: "GET",
beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},
success: function() { alert('Success!' + authHeader); }
});
Http://api.jquery.com/jQuery.ajax /
Http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-18 20:52:58
A partir de jQuery 1.5, hay un hash headers
que puede pasar de la siguiente manera:
$.ajax({
url: "/test",
headers: {"X-Test-Header": "test-value"}
});
De http://api.jquery.com/jQuery.ajax:
Headers (added 1.5): Un mapa de pares clave/valor de encabezado adicionales para enviar junto con la solicitud. Esta configuración se establece antes de que se llame a la función beforeSend; por lo tanto, cualquier valor en la configuración headers se puede sobrescribir desde la función beforeSend.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-11 15:00:07
$.ajax({
url: URL,
type: 'GET',
dataType: 'json',
headers: {
'header1': 'value1',
'header2': 'value2'
},
contentType: 'application/json; charset=utf-8',
success: function (result) {
// CallBack(result);
},
error: function (error) {
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-05 07:19:21