I am currently learning to use the wordpress CMS and I have a problem with the development of my plugin, I would need to send a data with the ajax post method, the problem is that the request is sent (status code 200, OK), but my targeted php page does not receive it .. Thank you in advance for your help =)
My code :
php file :
<?php
/**
* Plugin Name: Covid Tracker
**/
if(!defined('ABSPATH')){
define('ABSPATH', dirname(__FILE__) . '/');
}
include_once("db_CT.php");
register_activation_hook(__FILE__,"DBP_tb_create");
include_once("dataAdd.php");
register_activation_hook(__FILE__,"addDB");
add_shortcode('CoviT', 'CT');
function CT(){
echo ("
<div id='CT'>
<input type='hidden' id='dir' value='".plugin_dir_url(__FILE__).'filter.php'."'>
<input type='hidden' id='adminAjaxDir' value='".site_url().'/wp-admin/admin-ajax.php'."'>
<input type='text' value='' id='search' placeholder='Search department'>
<button id='submit' class='btn btn-primary'>Search</button>
<table class='blueTable'>
<thead>
<tr>
<th>Nom</th>
<th>Date</th>
<th>hospitalises</th>
<th>gueris</th>
<th>deces</th>
<th>nouvelles Hospitalisations</th>
<th>nouvelles Reanimations</th>
<th>reanimation</th>
</tr>
</thead>
<tbody id='tbody'>
</tbody>
</table>
</div>
");
wp_enqueue_script('appCT', plugin_dir_url(__FILE__) . 'appCT.js');
wp_enqueue_style('styleCT', plugin_dir_url(__FILE__) . 'styleCT.css');
wp_enqueue_script( 'ajax', plugin_dir_url(__FILE__) . 'ajax.js');
wp_localize_script( 'ajax', 'adminAjax', admin_url( 'admin-ajax.php' ) );
}
add_action('wp_ajax_nopriv_doAjax', 'get_data');
add_action('wp_ajax_doAjax', 'get_data');
function get_data(){
if(isset($REQUEST)){
$search = $REQUEST['value'];
echo 'Hello';
}
die();
}
?>
ajax.js :
function sendSearch(){
var search = document.getElementById('search').value;
jQuery(document).ready(function($) {
$.ajax({
url: '/integrationWP/wp-admin/admin-ajax.php',
method : 'POST',
data:{
'action':'doAjax',
'value': search
},
success: function(data){
// alert('?a fonctionne !');
},
error: function(errorThrown){
alert('error');
console.log(errorThrown);
}
});
});
}
appCT.js :
var main = document.getElementById('CT');
fetch('https://coronavirusapi-france.now.sh/AllLiveData')
.then(res => {
if(res.ok){
res.json().then(data => {
console.log(data);
var total = data['allLiveFranceData'].length;
for(var i = 0; i < total; i++){
var departement = data['allLiveFranceData'][i];
var name = departement['nom'];
var date = departement['date'];
var hospitalises = departement['hospitalises'];
var gueris = departement['gueris'];
var deces = departement['deces'];
var nouvellesHospitalisations = departement['nouvellesHospitalisations'];
var nouvellesReanimations = departement['nouvellesReanimations'];
var reanimation = departement['reanimation'];
var tbody = document.getElementById('tbody');
var tr = document.createElement("tr");
tr.id = 'tr'+(i+1);
tr.innerHTML = "<td>"+name+"</td><td>"+date+"</td><td>"+hospitalises+"</td><td>"+gueris+"</td><td>"+deces+"</td><td>"+nouvellesHospitalisations+"</td><td>"+nouvellesReanimations+"</td><td>"+reanimation+"</td>"
tbody.appendChild(tr);
}
})
}
})
document.getElementById('submit').addEventListener('click', function(){
sendSearch();
document.getElementById('search').value = "";
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…