Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-headers and $ajax headers option #49

Merged
merged 2 commits into from
Feb 9, 2024
Merged

Add x-headers and $ajax headers option #49

merged 2 commits into from
Feb 9, 2024

Conversation

imacrayon
Copy link
Owner

@imacrayon imacrayon commented Jan 16, 2024

Fixes #44

Use x-headers to set request headers, JavaScript expressions allowed:

<form method="post" action="/articles" x-headers="{'X-CSRFToken': '{{ csrf_token }}'}">

Use the headers option to set request headers for $ajax magics:

<div @click="$ajax({
  method: 'post',
  action: '/articles',
  headers: { 'X-CSRFToken': '{{ csrf_token }}'},
})">

This PR also adds an X-Alpine-Target header in every request which lists the IDs (space-separated) of all targets on the page:

<form method="post" action="/articles x-target="articles articles_count">

Issues a request with headers:

X-Alpine-Request: true
X-Alpine-Target: articles articles_count

This could be used to generate HTML fragments on the server.

@imacrayon imacrayon force-pushed the x-headers branch 2 times, most recently from ca67dd3 to a5ebe6e Compare February 9, 2024 19:56
@imacrayon imacrayon marked this pull request as ready for review February 9, 2024 19:56
@imacrayon imacrayon merged commit 5976933 into main Feb 9, 2024
2 checks passed
@imacrayon imacrayon deleted the x-headers branch February 9, 2024 19:59
@knowsuchagency
Copy link

Amazing. Thanks for this update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question on passing CSRF tags for Django
2 participants