It seems like you're trying to set the CSRF token in the headers for an AJAX request within the H5P standalone options. However, the way you're setting the headers might not be correctly interpreted by the H5P standalone library or the underlying AJAX mechanism it uses.
To ensure that the CSRF token is included in the headers of the AJAX request, you can try the following approach:
- Fetch the CSRF token from the meta tag as you've done.
- Modify the
ajaxproperty in theoptionsobject to include abeforeSendfunction. This function is called before the AJAX request is sent, allowing you to modify the headers.
Here's how you can modify your code:
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const el = document.getElementById('h5p-container');
const options = {
h5pJsonPath: 'h5p-content/true-false-question-34806/',
frameJs: 'h5p/frame.bundle.js',
frameCss: 'h5p/styles/h5p.css',
postUserStatistics: true,
ajax: {
setFinishedUrl: "{{ route('h5p.finish') }}",
// Add beforeSend function to set headers
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF-TOKEN', csrfToken);
xhr.setRequestHeader('_method', 'patch');
},
},
};
new H5PStandalone.H5P(el, options);
Please note that the beforeSend function receives the xhr object, which represents the XMLHttpRequest. You can use this object to set request headers.
Make sure that the H5PStandalone library supports the beforeSend function in its AJAX configuration. If it doesn't, you may need to modify the library itself or submit a feature request to the maintainers to support custom headers in AJAX requests.
Also, ensure that the route you're posting to (h5p.finish) is expecting a PATCH request and that the CSRF token is valid for the session. If the token is not valid, you will continue to receive a CSRF token mismatch error.