rog*_*nin 0 php wordpress jquery
AJAX Post没有在wordpress中返回成功呼叫。我有以下代码,可以进入测试中的第一个对话框,但是无论如何我都不会进入第二个对话框。functions.php即使我声明了它,也找不到函数。
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
Run Code Online (Sandbox Code Playgroud)
functions.php文件
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");
Run Code Online (Sandbox Code Playgroud)
好吧,我整理以下内容
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
Run Code Online (Sandbox Code Playgroud)
而且我的功能php就像不固定php函数一样。
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");
Run Code Online (Sandbox Code Playgroud)
问题是您的函数functionRedeempoints不会返回ajax调用可以处理的任何内容。
它甚至在return语句之前就死了。而且,PHP端的返回实际上不会由JS解释。JS只能从http请求读取,因此您实际上需要通过echo语句对其进行写入。
Wordpress为您提供了一种方便的处理方式:您需要的是:
function functionRedeempoints() {
wp_send_json_success(true);
}
Run Code Online (Sandbox Code Playgroud)
这已经可以停止执行并正确地对响应进行JSON编码了。
另外,JS端的正确响应处理与示例代码中的响应稍有不同。您可以在此处找到详细信息:https : //codex.wordpress.org/Function_Reference/wp_send_json_success
但归根结底是,成功编码在响应的result.success属性中。
因此,您希望您的支票是
if(response.success)
Run Code Online (Sandbox Code Playgroud)
代替 if(response.type == "success")
通过这些更改,您的示例应该可以运行:)
根据您的代码的工作示例(以插件形式):
将其放在plugins文件夹中的hello.php中
<?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">';
}
Run Code Online (Sandbox Code Playgroud)
将其放在plugins文件夹中的hello.js中
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
}
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助您从这里开始,单击管理屏幕左上角应显示的按钮就可以正常工作(放大;))