Adding Authorization header to ajax calls?

Forums: 

Hello All.

A page with a 'Fill in the Blanks' content-type is trying to POST to 'saveResult' on the server.  My server's REST API requires an Authorization header to be included in order to process the request.

Can anyone tell me where I can hook into the ajax call to add to the header?

Thanks!

Corey.

Content types: 

Not an easy question, it seems.

Looking further, H5P uses it's own jQuery (H5P.jQuery) and jQuery has an ajaxSetup() method where I can set request defaults that are added to every request.  So where would be a place that I can insert javascript when H5P 'starts up'? 

Is there any events that I can hook into for iframe and div-based content types where I can execute a H5P.jQuery.ajaxSetup()?

I don't want to hack the H5P source because I'll lose it on the next update.

Thanks.

otacke's picture

Hi Corey!

You can use the alter_scripts hook. Please check thr documentation at https://h5p.org/node/2692 for details for your host system.

Best,

Oliver

I'm trying to integrate H5P into a custom host - it's been a (almost) futile exercise. ;) So, there doesn't appear to be any alter_scripts capability.

thomasmars's picture

What do you mean by "custom host" ? Have you written your own H5P integration ?
If you're using an existing integration the instructions are, like otacke said, here: https://h5p.org/node/2692.
If you have your own integration you have to add the scripts where you add the library files in your integration. Should be in alterLibraryFiles and when adding files to the view of H5Ps.

By custom host, I'm not meaning Moodle, Wordpress, etc.  It's an in-house developed SPA website that needs to load the H5P infrastructure on-demand.

Turns out I can call into H5P.jQuery ajaxSetup to get an Auth header to be added to every ajax call from that point on.  Took me a very long time to figure that out - namely when H5P.jQuery is loaded (and stable) to be able to make this call.