简体   繁体   English

Wordpress Ajax发布不起作用无法捕获信息

[英]Wordpress ajax post not working can't catch information

I have the following ajax post being made in my wordpress: 我的wordpress中有以下ajax帖子:

(function($) {
$('.selectas').change(function() {

        action = 'post_selectas',
        data = {option : selectedValue};

        jQuery.post(ajaxurl, data, function(response) {
            console.log("Duomenys issiusti !" + response);
        });
    });
 })(jQuery);

Then ajax posting in this page, console says its working but I can't catch info...here is all my functions 然后在此页面上发布ajax,控制台说它可以正常工作,但我无法捕获信息...这是我的所有功能

    function select_skript() {
    if (is_admin() ) {
        $scriptsrc = get_stylesheet_directory_uri() . '/dist/scripts/';
        wp_register_script( 'selectas', $scriptsrc . 'selectas.js', 'jquery', '1.0',  true );
        wp_enqueue_script( 'selectas' );

        $translation_array = array( 'templateUrl' => get_stylesheet_directory_uri() );
        wp_localize_script( 'selectas', 'selectas_js', $translation_array );
    }
}

add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\select_skript' );

function post_selectas(){

    $id_nr = $_REQUEST['data'];
    $ids = $_POST['data'];

    return "$id_nr or $ids";
    return "test";

  }

function not returning any data test or ids.... where is the problem??? 函数不返回任何数据测试或ID。...问题出在哪里???

Try with this: 试试这个:

<?php

add_action( 'admin_enqueue_scripts', 'select_skript' );

function select_skript() {
    if (is_admin() ) {
        $scriptsrc = get_stylesheet_directory_uri() . '/dist/scripts/';
        wp_register_script( 'selectas', $scriptsrc . 'selectas.js', 'jquery', '1.0',  true );
        wp_enqueue_script( 'selectas' );

        $translation_array = array(
            'templateUrl' => get_stylesheet_directory_uri(),
        );
        wp_localize_script( 'selectas', 'selectas_js', $translation_array );
    }
}

add_action( 'wp_ajax_post_selectas', 'post_selectas_callback' );

function post_selectas_callback(){

    $id_nr = $_REQUEST['data'];
    $ids = $_POST['data'];

    // return "$id_nr or $ids"; // the or in return won't work; see http://stackoverflow.com/a/14394447/629127
    echo "test";

    die();
}

According to AJAX in Plugins you need to hook your function to wp_ajax_{function_name} hook. 根据插件中的AJAX,您需要将函数挂钩到wp_ajax_{function_name}挂钩。

add_action( 'wp_ajax_post_selectas', 'post_selectas_callback' );

And you're missing die() (or wp_die() , should be the same) to terminate immediately and return a proper response. 而且您缺少die() (或wp_die()应该相同)来立即终止并返回正确的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM