[英]jQuery AJAX request returns NULL from PHP
Here is the code I am working on for my Wordpress site: 这是我为Wordpress网站工作的代码:
jQuery: jQuery的:
jQuery(document).ready(function($)
{
var actionValue;
$(".tabRow li").on('click', function(event)
{
event.preventDefault(); //override default behaviour
var clicked = $(this); //caches click so we don't scan DOM again
if(clicked.attr('id')=="tabAddData") //tab1 clicked
{
actionValue = "tab1Clicked";
}
$("li").removeClass("selected");
clicked.addClass("selected");
alert ('starting ajax call');
$.ajax(
ajaxObject.ajaxurl, //request gets sent to this url
{ //start of [options]
type: 'POST',
dataType: 'json', //type of data expected back from server
//data to send to server (JSON format)
data:
{
action: 'ajaxAction',
nonce: ajaxObject.nonce,
actionName: actionValue
}
} //end of [options]
) //end ajax(), the other fuctions are chained to it (.done .fail .always)
.done (function(data)
{
alert ('success function');
if(actionValue == "tab1Clicked") //tab1 clicked
{
$('#dataSection').empty();
$('#dataSection').append(data);
}
}) //end of done (success) function
.fail (function(xhr, desc, err)
{
alert ('error function');
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}) //end of fail (error) function
}); //end of onclick
});
PHP: PHP:
<?php
$my_action='ajaxAction';
if(defined('DOING_AJAX') && DOING_AJAX)//check if AJAX is loaded and working
{
//for logged in users
add_action('wp_ajax_'.$my_action, 'ajaxResponse');
}
function ajaxResponse()
{
if(wp_verify_nonce($_POST['nonce'], 'ajaxAction'))
{
if($_POST['actionName'] == "tab1Clicked")
{
$addDataSection = getAddDataSection();
$response=array(
'status' => 'success',
'addDataSection' => $addDataSection
);
header('Content-type:application/json;charset=utf-8');
echo json_encode($response);//encodes the jQuery array into json format
die;
}
}
}
function getAddDataSection()
{
//lots of echo statements which builds the html code
}
?>
When I first load my page, my PHP function getAddDataSection()
generates the HTML inside my <div id='dataSection>
. 第一次加载页面时,我的PHP函数getAddDataSection()
在<div id='dataSection>
内生成HTML。 This works fine. 这很好。
When I click on tab1, my jQuery AJAX call is supposed to reuse the same PHP function to generate my HTML. 当我单击tab1时,我的jQuery AJAX调用应该重用相同的PHP函数来生成HTML。 This is not working fine. 这不能正常工作。
After I click on tab1, the jQuery fail function is triggered. 单击tab1后,将触发jQuery失败函数。
When I check Firebug, the response data contains the html generated by my PHP function getDataSection()
, followed by a JSON string 当我检查Firebug时,响应数据包含由我的PHP函数getDataSection()
生成的html,后跟一个JSON字符串
{"status":"success","addDataSection":null}
When replying, keep in mind I'm a newbie. 回复时,请记住我是新手。 Thanks :) 谢谢 :)
Updated to include Console error: 更新以包括控制台错误:
Details: parsererror
Error:SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
I think I found a solution. 我想我找到了解决方案。
In my jQuery, I changed the dataType from json to html. 在我的jQuery中,我将dataType从json更改为html。
Then in my PHP, I changed this: 然后在我的PHP中,我对此进行了更改:
if($_POST['actionName'] == "tab1Clicked")
{
$addDataSection = getAddDataSection();
$response=array(
'status' => 'success',
'addDataSection' => $addDataSection
);
header('Content-type:application/json;charset=utf-8');//still works without this line
echo json_encode($response);//encodes the jQuery array into json format
die;
}
to this: 对此:
if($_POST['actionName'] == "tab1Clicked")
{
$addDataSection = getAddDataSection();
echo $addDataSection;
die;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.