weixin_33711641 2014-06-15 06:23 采纳率: 0%
浏览 8

wp前端中的ajax

I want to use ajax in wp plugin front end not the admin pages, I used wp_localize_script() to be able to access ajaxurl in front end and it works. Here is my ajax code

$j = jQuery.noConflict();

function commonProvinceChange() {

    $j(".province").change(function() {
       var selectedProvinceId = $j(this).val();
       var districts = "<option disabled selected='selected'>Select District</option>";
       $j.post(naqdina_map.ajaxurl, {
         province_id : selectedProvinceId,
         action : 'province_districts'
        }, function(data, status) {
          alert(naqdina_map.ajaxurl);
                            data = JSON.parse(data);
                            $j(".district").html(districts);
                            for ( var i = 0; i < data.length; i++) {
                                $j(".district").append(
                                        "<option value=" + data[i].id + ">"
                                                + data[i].district_name
                                                + "</option>");
                            }// for
                        });

                    });
}// commonProvinceChange()

and on my plugin php file

function naqdina_front_script() {

    wp_enqueue_script ( 'jquery' );

    wp_register_script ( 'naqdina_map_script', plugins_url ( 'naqdina_map/map.js', __FILE__ ), array (
            'jquery' 
    ) );
    wp_localize_script('naqdina_map_script','naqdina_map',array('ajaxurl'=>admin_url('ajax-admin.php')));
    wp_enqueue_script ( 'naqdina_map_script' );
}//naqdina_front_script()

add_action ( 'wp_enqueue_scripts', 'naqdina_front_script' );

and then

add_action ( 'wp_ajax_province_districts', 'province_districts' );

function province_districts() {

    global $wpdb;

        $province_id = $_POST ['province_id'];

    $pname = $wpdb->get_results ( "select d.id,d.name as district_name from naqdina_districts as d join naqdina_provinces as p on(d.province_id=p.id) where p.id='" . $province_id . "'" );

    echo json_encode ( $pname );
    die (); // use die() to prevent further content

} // province_districts()

here the post() method in my js code above returns html code of the whole page not the the response echoed by province_districts() function.

Could someone help??

  • 写回答

2条回答 默认 最新

  • weixin_33725239 2014-06-15 08:59
    关注

    You need to use add_action ( 'wp_ajax_nopriv_province_districts', 'province_districts' ); to use AJAX in the frontend.

    EDIT: In map.js on line 9 you are POSTing to "" which resolves as a relative URL to the current page. If you replace that with naqdina_map.ajaxurl your code will work.

    Edit: This is what you need to do.

    评论

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区