Why is my ajax request getting response 0?

后端 未结 3 1728
既然无缘
既然无缘 2020-12-10 04:14

I\'ve setup the basic wordpress ajax example in my wp theme. The trigger is made by modernizr.js checking the media queries on the page.

jQuery(document).rea         


        
3条回答
  •  孤街浪徒
    2020-12-10 04:42

    Add the "exit" end of the function as shown below, this will fix the return 0 with the response in WordPress on using the ajax request.

    add_action('wp_ajax_nopriv_getStateList', 'getStateList');
    add_action('wp_ajax_getStateList', 'getStateList');
    
    function getStateList() {
        global $wpdb;
        $countryId = $_POST['countryId'];
        $results = $wpdb->get_results("SELECT id,name FROM regions where country_id ='".$countryId."' ");
        echo json_encode(array('status'=>200,'data'=>$results));
        exit;
    }
    

提交回复
热议问题