Search code examples
ruby-on-railsjqueryprototypejs

set request header with protoype


I want to remake the following jquery code to prototype :

jQuery.ajax({ url:url,
          type: 'POST',
          beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', jQuery('meta[name="csrf-token"]').attr('content'))},
          data: { 'value': value } , 
          success: function(response) {
          }
        });

Ii tried this but the request header is missing so how can i set it

new Ajax.Request(url , {
    parameters: { 'value': value },
    onSuccess: function(transport) {
    }
  })

Solution

  • use the requestHeaders option http://prototypejs.org/doc/latest/ajax/

    new Ajax.Request(url , {
      parameters: { 'value': value },
      requestHeaders: { 'X-CSRF-Token', ... },
      onSuccess: function(transport) {}
    })