没有从ajax发布返回成功

没有从ajax发布返回成功

本文介绍了没有从ajax发布返回成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AJAX Post没有在wordpress中返回成功呼叫.我有以下代码,可以进入测试中的第一个对话框,但是无论如何我都不会进入第二个对话框.即使已声明,也无法在functions.php中找到该函数.

AJAX Post is not returning success call in wordpress. I have the following code and I can get to the first dialog box in testing, but no mater what I do its not getting to the second. It's not finding the function in functions.php even though I have it declared.

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

    var datastring = $("#redemmpointsForm").serialize();

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

          jQuery.ajax({
             type : "post",
             dataType : "json",
             url : myAjax.ajaxurl,
             data : {"action": "redeempoints", "points":points},
             success: function(response) {
                if(response.type == "success") {
                 alert('do i get here');
                }
                else {
                   // Do something else
                }
             }
          });
    });
     }); //Modal event Ends

functions.php文件

functions.php file

wp_localize_script( 'inkthemes', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));

function functionRedeempoints() {

die();
return true;
}

add_action("wp_ajax_functionRedeempoints", "functionRedeempoints");
add_action("wp_ajax_nopriv_functionRedeempoints", "functionRedeempoints");

好吧,我仔细阅读以下内容

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

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

         var allData = {
   action: 'functionRedeempoints',
   points: points,
   comments:comments
}
       var data = JSON.stringify(allData);

           alert( 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
            }
         }
      });
});
 }); //Modal event Ends

还有我的功能php就像没有找到php函数一样.

And iN MY FUCNTIONS php Its like its not fidning the php function.

  wp_localize_script( 'inkthemes', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));

function functionRedeempoints() {
wp_send_json_success(true);
}

add_action("wp_ajax_redeempoints", "functionRedeempoints");
add_action("wp_ajax_nopriv_redeempoints", "functionRedeempoints");

推荐答案

问题是您的函数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 ;) )

这篇关于没有从ajax发布返回成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:18