To elaborate on my comments, your issue is here:
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({
action: 'cdnjs_add_library',
library: library
}),
When sending data to admin-ajax.php
you need to set the action
property of data
to the action name, eg. cdnjs_add_library
. That’s not what you’re doing here. data
is not an object with an action
property, as is expected, it’s a string containing JSON that happens to include an action
property. These are not the same thing.
In the source of admin-ajax.php
you’ll see this:
$action = $_REQUEST['action'];
If you post JSON to admin-ajax.php $_REQUEST
will not have an action
property, for reasons I explain here. This means that your action will not be fired and your PHP code will not run.
The solution is to remove contentType
and pass the data like so:
data: {
action: 'cdnjs_add_library',
library: library
},
Your cdnjs_add_library_callback()
function will then need to set $libary
like this:
$library = $_POST['library'];