First you need to to send function name as additional data as
$.ajax({
type: "POST",
url: compareids_ajax.ajax_url,
data: { // Data object
compareIDs : compareIDs,
action: 'your_ajax_function' // This is required to let WordPress know which function to invoke
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
console.log( msg );
},
error: function (errormessage) {
console.log( errormessage );
}
});
In your php file
add_action('wp_ajax_nopriv_your_ajax_function','your_ajax_function'); // Ajax Function should be hooked like this for unauthenticated users
add_action('wp_ajax_your_ajax_function','your_ajax_function'); //
function your_ajax_function(){
$comparedIds = $_POST['compareIDs']; // Your data is now available in $_POST
// Do your stuff here
die(); // At the end
}
See wp_ajax_(action)