且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有从ajax发布返回成功

更新时间:2021-10-12 22:58:38

问题是您的函数functionRedeempoints不会返回ajax调用可以处理的任何内容.

The problem is that your function functionRedeempoints does not return anything that the ajax call can handle.

它甚至在return语句之前就死了. 同样,PHP端的返回实际上不会由JS解释. JS只能从http请求中读取内容,因此您实际上需要通过echo语句对其进行写入.

It just dies even before the return statement. Also a return by the PHP end will never actually be interpreted by the JS. JS can only read from the http request, so you need to actually write to it by an echo statement.

Wordpress为您提供了一种方便的方式来处理此问题: 您需要的是这样的东西:

Wordpress provides a convenient way of handling this for you: What you need would be something like:

function functionRedeempoints() {
   wp_send_json_success(true);
}

这已经可以停止执行并正确地对您的响应进行JSON编码了.

This already takes care of stopping execution and properly JSON encoding your response.

此外,JS端的正确响应处理与示例代码中的处理略有不同. 您可以在此处找到有关此内容的详细信息: https://codex.wordpress.org/Function_Reference/wp_send_json_success

Also the correct response handling on the JS side is a little different than in your example code. You can find the details on this here: https://codex.wordpress.org/Function_Reference/wp_send_json_success

但是归根结底是,成功编码在响应的result.success属性中.

But what it boils down to is that the success is encoded in the result.success property of the response.

因此,您希望支票成为

if(response.success)

而不是if(response.type == "success")

通过这些更改,您的示例应该可以运行:)

With these changes your example should work though :)

基于您的代码的工作示例(以插件形式):

Working example ( in plugin form) based on your code:

将此内容放入plugins文件夹的hello.php中

Put this in hello.php in the plugins folder

<?php

/*
Plugin Name: Ajax demo
*/

function test_load_js() {
    wp_register_script( 'ajax_demo', plugins_url( 'hello.js' ), array( 'jquery' ) );
    wp_localize_script( 'ajax_demo', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    wp_enqueue_script( 'ajax_demo' );
}

function functionRedeempoints() {

    wp_send_json_success( true );
}

add_action( "wp_ajax_functionRedeempoints", "functionRedeempoints" );
add_action( "wp_ajax_nopriv_functionRedeempoints", "functionRedeempoints" );
add_action( "init", "test_load_js" );
if ( ! defined( 'DOING_AJAX' ) ) {
    echo '<input type=button value="send" id="send_btn">';
}

将此内容放入plugins文件夹的hello.js中

Put this in hello.js in the plugins folder

jQuery(document).ready(function () {
    jQuery("#send_btn").click(function () {

        var points = jQuery('#points').val();
        var comments = jQuery('#comments').val();

        var data = {
            action: 'functionRedeempoints',
            points: points,
            comments: comments
        };

        alert(JSON.stringify(data));

        jQuery.ajax({
            type: "post",
            dataType: 'json',
            url: MyAjax.ajaxurl,
            data: data,
            success: function (response) {
                if (response.success) {
                    alert('do i get here');
                }
                else {
                    // Do something else
                }
            }
        });
    });
});

希望这可以帮助您从这里开始,当您单击应显示在管理屏幕左上角的按钮(放大;)时,效果很好.

Hope this helps you to get a start here, works just fine when you click the button that should appear on the upper left of your admin screen ( zoom in ;) )