I am trying to develop a wooCommerce extension for verifying license key one wordpress server to another wordpress server.I wrote the following code for the verification. This code is not working perfectly. Please check the following script and correction it. After form submit console.log showing the following error
admin.php:1 Access to XMLHttpRequest at ‘https://server2.net/wp-admin/admin-ajax.php’ from origin ‘https://server1.net’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
wordpress server one.This is HTML form for submitting the license key.
<tr>
<td width="20%"><label>License Key</label></td>
<td width="80%"> <input type='text' name='lincesekey' value=''> </td>
</tr>
<tr>
<td width="20%"><input type="submit" name="licensekey" id="licensekey"
class="button button-text" value="<?php esc_html_e('Active License', 'orange-smtp');?>"> </td>
<td width="70%"> </td>
</tr>
</table>
</form>
jQuery(document).ready(function($) {
jQuery('#licenseForm').submit(function(e) {
e.preventDefault(); // Prevent the form from submitting normally
//var licenseKey = jQuery('#licenseKey').val();
var formData = jQuery(this).serialize();
// AJAX request to host server
jQuery.ajax({
type: 'POST',
url: 'https://ecom.lifestyleblogs.net/wp-admin/admin-ajax.php',
data: formData + '&action=licensekey_process_received_data', // Include action parameter
success: function(response) {
console.log('Data sent successfully');
// Handle success response
},
error: function(xhr, status, error) {
console.error('Error sending data:', error);
// Handle error response
}
});
});
});
wordpress server two.I wrote the following code to receive the license code from wordress server one for verify the license key.
public function licensekey_process_received_data(){
if(isset($_GET['licenseKey'])) {
$licenseKey = $_POST['licenseKey'];
// Sanitize the license key input (optional, but recommended)
$licenseKey = sanitize_text_field($licenseKey);
global $wpdb;
$table_name = $wpdb->prefix . 'license_key';
$validLicenseKeys = $wpdb->get_col("SELECT glicense_key FROM $table_name");
if (in_array($licenseKey, $validLicenseKeys)) {
//return true; // License key is valid
$response = array(
'status' => 'success',
'message' => 'License key is valid.'
);
} else {
//return false; // License key is invalid
$response = array(
'status' => 'error',
'message' => 'License key is invalid.'
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
}
I was try to fix various way but i failed to resolve this issue.