Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我在WordPress中使用Ajax时在控制台中收到400(错误请求)通知?_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Php 为什么我在WordPress中使用Ajax时在控制台中收到400(错误请求)通知?

Php 为什么我在WordPress中使用Ajax时在控制台中收到400(错误请求)通知?,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,注意我进入控制台: add_action( 'wp_ajax_my_action', 'my_action' ); function my_action() { global $wpdb; // this is how you get access to the database $whatever = ( $_POST['post_id'] ); //$whatever += 10; echo $whatever; wp_die();

注意我进入控制台:

add_action( 'wp_ajax_my_action', 'my_action' );

function my_action() {
    global $wpdb; // this is how you get access to the database

    $whatever = ( $_POST['post_id'] );

    //$whatever += 10;

        echo $whatever;

    wp_die(); // this is required to terminate immediately and return a proper response
}
职位

400(错误请求)

Functions.php:注册JS文件并本地化脚本

function my_scripts_method() {



      wp_register_script('custom_script',
        get_stylesheet_directory_uri()  . '/js/jquery_test.js',
       array('jquery'),
       '1.0' );

      wp_enqueue_script('custom_script');


      wp_localize_script( 'custom_script', 'custom_script_object', array(
            'ajax_url' => admin_url( 'admin-ajax.php' )
        ));

            }
    add_action('wp_enqueue_scripts', 'my_scripts_method');
jquery\u test.js文件:

var $j = jQuery.noConflict();

$j(function(){

    $j(".small-board-profile-member").click(function(){

  var fgfdgds =  $j(this).attr('value');
console.log(fgfdgds);


$j('.modal-body').attr('value', fgfdgds);

 $j.ajax({
    url : custom_script_object.ajax_url,
    type : 'post',
    data : {
        post_id : fgfdgds
    },
    processData: false,
    contentType: false,
    success : function( response ) {
        $j('.rml_contents').html(response);
        console.log("it worked");
    }
}); 



  });
});
处理ajax请求的MyPHP函数(放入functions.php):

add_action( 'wp_ajax_my_action', 'my_action' );

function my_action() {
    global $wpdb; // this is how you get access to the database

    $whatever = ( $_POST['post_id'] );

    //$whatever += 10;

        echo $whatever;

    wp_die(); // this is required to terminate immediately and return a proper response
}
我认为问题在于处理ajax请求的php函数。非常感谢您的时间

编辑:

在javascript中更改此选项:

var $j = jQuery.noConflict();

$j(function () {

    $j(".wpb_wrapper").click(function () {

        var fgfdgds = $j(this).attr('value');

        var formData = new FormData();
        formData.append("post_id", 'fgfdgds');
        formData.append("action", 'my_action');


        $j('.modal-body').attr('value', fgfdgds);

        $j.ajax({
            url: custom_script_object.ajax_url,
            type: 'post',
            data: formData,
            processData: false,
            contentType: false,
            success: function (response) {
                $j('.rml_contents').html(response);
                console.log("it worked");
            }
        });



    });
});

非常感谢你。我没有看到
add_操作('wp_-ajax_-post_-request','function_-to_-call')
在WordPress的抄本中?非常感谢你,我已经找到并编写了它,但是得到了相同的错误。如果你能看看这段代码,我会非常感激,我已经把它添加到我的问题中了。非常感谢您抽出时间!我已经更新了Javascript,它应该可以工作了;现在,您只需要添加wp_ajax_nopriv,让非管理员用户使用ajax谢谢您的时间,但它仍然不起作用。现在在网站上查看此特定页面时,它也会将我从WordPress管理员中注销,这在以前从未发生过。只需添加add_操作('wp_ajax_nopriv_my_action','my_action')@mujeebu rahman,你能看看我的php函数来处理ajax请求吗?我想离那里很近&我会非常感激的。谢谢你的时间。因为你的要求不好。您使用的
data
processData
contentType
的组合毫无意义。@KevinB请,您能发布一个答案吗?我真的被这件事缠住了&我将不胜感激。谢谢你的时间。