我的AJAX无法发送数据,请更正我的代码

时间:2019-01-08 作者:Gufyand D

首先请原谅我的英语。因此,我构建了自己的WordPress主题,并希望在其中使用ajax,我正在使用ajax在我的帖子循环内部函数中更改帖子类别。php,所以当一个人单击索引中的按钮时。php ajax将发送数据和函数。php将更改类别,问题是我无法访问admin ajax。php

SO IN HERE MY CODE IS JUST FOR TESTING IF THE AJAX WORK OR NOT, IT WONT ECHO THE POST THAT HAD BEEN SENT

This is my .js file that contains ajax

  function kategori(kategori2){
      //alert(kategori2);
      jQuery.ajax({
        url: MyAjax.ajax_url,
        type: "POST",
        data: { action: "berubah", kategori: "berita"},
        success : function(data) {

        }
      });

  }
This is my function.php

<?php

// Support Featured Images
add_theme_support( \'post-thumbnails\' );

add_action( \'wp_enqueue_scripts\', \'my_script_enqueuer\' );

function my_script_enqueuer() {
    wp_register_script( \'add-order-front\',  get_template_directory_uri() . \'/js/programku.js\' );
    wp_localize_script( \'add-order-front\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
    wp_enqueue_script( \'add-order-front\' );

}

add_action( \'wp_ajax_berubah\', \'berubah\' );
add_action( \'wp_ajax_nopriv_berubah\', \'berubah\' );

function berubah()
{
    $kategori = isset( $_POST[\'kategori\'] ) ? $_POST[\'kategori\'] : \'\';
    echo $kategori;
    wp_die();
}


我也是javascript新手,我使用Mozilla控制台

\'引用错误:未定义ajax\\u对象\'



EDIT #1 -

我可以访问admin ajax。php现在,我的错误是我没有标题。php关于我的主题,抱歉,我是wordpress的新手

现在我的问题是,function.php can\'t get the data sent by AJAX from programku.js and response in console is an HTML code

<小时/>in my theme i just have 5 files

索引。php编程。js文件夹标题中的js。php页脚。php和最后一个函数。phpIs there is a files that important in wordpress theme developing that i left?




enter image description here

enter image description here

2 个回复
SO网友:coolpasta

function kategori(){
    //alert(kategori2);
    jQuery.ajax({
        url: MyAjax.ajax_url,
        type: "POST",
        data: {
            action: "berubah",
            kategori: "berita"
        },
        success : function(response) {
            console.log(response);
            //Acess whatever you sent from the back-end with response.data.%var_name%
        },
        error: function(data ) {
            console.log(response);
        }
    });
}

function berubah()
{
    $kategori = $_POST[\'kategori\'];
    wp_send_json_success( $kategori, 200 );
}
使用wp_send_json_success.

我得到的回应:

{"success":true,"data":"berita"}

SO网友:maverick

根据Gufyand D的要求,这里是wordpress中的简单ajax请求(基于问题)

***place this code inside your js file***
(function($){
    $(\'div#element_id\').on(\'click\', function(){
    // here idea is to get values from page using js to send them as data e.g. var cat_name = you can get category name using jquery.

    $.ajax( {
        type: \'post\',
        url: MyAjax.ajax_url,
        data: {
            action: \'berubah\',
            cat_name: var_name // if you used a js var to store category name, just type variable name else if you want to use static value then wrap them in quotes like "category_name".
        },
        success: function(response){
            console.log(response);
        }
    } );
 } );
})(jQuery);
这是查看ajax的最低javascript代码(console.log)

现在functions.php 文件

注意:您应该知道您正在更改哪个主题(父主题或子主题;因为排队脚本过程不同)。以下代码用于父主题的函数。php文件

add_action( \'wp_enqueue_scripts\', \'my_script_enqueuer\' );

function my_script_enqueuer() {
    wp_register_script( \'add-order-front\',  get_template_directory_uri() . \'/js/programku.js\' );
    wp_localize_script( \'add-order-front\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
    wp_enqueue_script( \'add-order-front\' );

}

add_action( \'wp_ajax_berubah\', \'berubah\' ); // this is for logged in users
add_action( \'wp_ajax_nopriv_berubah\', \'berubah\' ); // this is for non logged in users

function berubah()
{
    // always check if the value is set or not ( I like ternery operator to do this )
    $kategori = isset( $_POST[\'cat_name\'] ) ? $_POST[\'cat_name\'] : \'\'; // you need to match the data sent from js file to this file i.e., the variable name used in the js file to send category name should match the array key you want to retrieve using POST superglobal
    echo $kategori;
    wp_die(); // always terminate ajax request after finishing your purpose i.e., wp_die() will prevent further execution of the script ( just in case )
}
要检查响应,请参见屏幕截图:![enter image description here

最终注释:

确保您的脚本可用于正在查看的页面开发时,始终打开WP\\u DEBUG,希望这对您有所帮助

干杯

相关推荐

尝试在WordPress中实现AJAX注释,遇到WP错误

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:下面是我对ajax的评论。js文件如下所示: * Let\'s begin with validation functions */ jQuery.extend(jQuery.fn, { /* * check i