questo funziona, ma viene fermato perché manca un token di autenticità:Modo corretto per inviare un token di autenticità con AJAX ..?
$(".ajax-referral").click(function(){
$.ajax({type: "POST", url: $(this).parent("form").attr("action"), dataType: "script"});
return false;
});
Così ho provato ad aggiungere in questo modo:
$(".ajax-referral").click(function(){
$.ajax({type: "POST", url: $(this).parent("form").attr("action") + "?&authenticity_token=" + AUTH_TOKEN, dataType: "script"});
return false;
});
E passa l'auth_token correttamente come param, ma sembra perdere il resto della mia forma.
Ad ogni modo è possibile inviare sia i dati del modulo che funzionano, sia il token di autenticità?
Questo è un ambiente rotaie. E ho questo nella mia testa.
= javascript_tag "var AUTH_TOKEN = '#{form_authenticity_token}';" if protect_against_forgery?
Le cose che ho provato
1.
= hidden_field :authenticity_token, :value => form_authenticity_token
2.
$.ajax({type: "POST", url: $(this).parent("form").attr("action"), dataType: "script", authenticity_token: AUTH_TOKEN});
3.
// Always send the authenticity_token with ajax
$(document).ajaxSend(function(event, request, settings) {
if (settings.type != 'GET') {
settings.data = (settings.data ? settings.data + "&" : "")
+ "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
}
});
Ho dovuto usare encodeURIComponent (AUTH_TOKEN), ma usandolo nella stringa di query ha funzionato una volta fatto. – Ken
il token non deve essere inviato nell'URL! – Gacci