Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

Problem for post data in ajax on wordpress plugin

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 :


function sendSearch () {

    document.getElementById('submit').addEventListener('click', function(){

        var search = document.getElementById('search').value;
        var dir = document.getElementById('dir').value;
        ////dir is equal to :
           <input type='hidden' id='dir' value='".plugin_dir_url(__FILE__).'filter.php'."'>                                                        
        //// 

        let xmlhttp = new XMLHttpRequest();
        xmlhttp.open("post", dir);
        xmlhttp.send(search);

        document.getElementById('search').value = "";
    })

}

sendSearch();


question from:https://stackoverflow.com/questions/65641444/problem-for-post-data-in-ajax-on-wordpress-plugin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tried to do as on the codex but I must have a little problem somewhere .. Same problem as above, the data is sent but never reaches its destination.. Thanks for your help =)

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);
                }
            });
        });
    
    }

    document.getElementById('submit').addEventListener('click', function(){
        sendSearch();
        document.getElementById('search').value = "";
    })

PHP file :

  `    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();
        }

`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...