簡體   English   中英

Wordpress使用Wordpress將Ajax值傳遞到特定頁面

[英]Wordpress passing ajax value to a specific page using Wordpress

我想將變量傳遞到特定頁面。 我找到了一個簡單的示例來說明如何在Wordpress中使用Ajax。

JavaScript的:

jQuery(document).ready(function($) {

// We'll pass this variable to the PHP function example_ajax_request
var fruit = 'Banana';

// This does the ajax request
$.ajax({
    url: ajaxurl,
    data: {
        'action':'example_ajax_request',
        'fruit' : fruit
    },
    success:function(data) {
        // This outputs the result of the ajax request
        console.log(data);
    },
    error: function(errorThrown){
        console.log(errorThrown);
    }
});  

});

插入到functions.php一段PHP

function example_ajax_request() {

// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {

    $fruit = $_REQUEST['fruit'];

    // Let's take the data that was sent and do something with it
    if ( $fruit == 'Banana' ) {
        $fruit = 'Apple';
    }

    // Now we'll return it to the javascript function
    // Anything outputted will be returned in the response
    echo $fruit;

    // If you're debugging, it might be useful to see what was sent in the $_REQUEST
    // print_r($_REQUEST);

}

// Always die in functions echoing ajax content
  die();
 }

add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );


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

不幸的是我不能傳遞變量。 我檢查了代碼,並得到以下錯誤:

Error: ajax_object is not defined

您是否知道另一種獲得相同結果的方法?

您距離很近,但缺少一些小東西……

我在評論中的意思是,您需要在兩種方式中都使用'ajax-script'來使用它:

add_action('wp_enqueue_scripts', 'add_js_scripts'); 
add_js_scripts(){
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
}

$_REQUEST更改$_REQUEST $_POST

function example_ajax_request() {

    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_POST) ) {

        $fruit = $_POST['fruit'];

        // Let's take the data that was sent and do something with it
        if ( $fruit == 'Banana' ) {
            $fruit = 'Apple';
        }

        // Now we'll return it to the javascript function
        // Anything outputted will be returned in the response
        echo $fruit;

        // If you're debugging, it might be useful to see what was sent in the $_POST
        // print_r($_POST);

    }

    // Always die in functions echoing ajax content
      die();

 }

添加了add_action( 'wp_ajax_nopriv_ … )

add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' ); // <= this one
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );

對於您的jQuery腳本script.js文件,有2個重要的小事項:

jQuery(document).ready(function($) {

    /* We'll pass this variable to the PHP function example_ajax_request */
    var fruit = 'Banana';

    /* This does the ajax request */
    $.ajax({
        url: ajax_object.ajaxurl, /* <====== missing here */
        type : 'post', /*    <========== and missing here */
        data: {
            'action':'example_ajax_request',
            'fruit' : fruit
        },
        success:function(data) {
            /* This outputs the result of the ajax request */
            console.log(data);
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });  

});

現在應該可以工作了……

參考文獻:

您使用wp_localize_script錯誤。 在PHP代碼中,刪除wp_localize_script行。

(可選)您可以(並且應該)添加以下內容

// this line is for users who are not logged in
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

一切都正確,除了必須在URL中添加ajax_object.ajax_url而不是ajaxurl:$ .ajax函數的參數為

jQuery(document).ready(function($) {

// We'll pass this variable to the PHP function example_ajax_request
var fruit = 'Banana';

// This does the ajax request
$.ajax({
    url: **ajax_object.ajax_url**,
    data: {
        'action':'example_ajax_request',
        'fruit' : fruit
    },
    success:function(data) {
        // This outputs the result of the ajax request
        console.log(data);
    },
    error: function(errorThrown){
        console.log(errorThrown);
    }
});  

});

復制並粘貼上面的腳本代替$ .ajax函數如果您想查看什么是ajax_object.ajax_url,則在它的代碼中對其進行警告,並警告您在wordpress中使用ajax所需的管理ajax文件的路徑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM