我有一个在单击按钮时通过 AJAX 更新 HTML 的系统。有一个函数可以获取要更新的 HTML(测试和工作),一个函数发送一个 JSON 编码的数组,其中包含一个真或假变量和一个字符串,其中包含来自另一个函数的更新的 HTML,最后是 JQuery 本身,它检索信息。问题是当我尝试通过数组传递 HTML 信息时,会出现“Uncaught SyntaxError: Unexpected token <”
检索和发送基本信息的 JQuery 代码
//One of the functions didn't have the data type for testing purposes
(function ($) {
$('.arrow-up').click(function () {
var user_id = $(this).attr('rel');
var my_data = {
action: 'arrow_handler', // This is required so WordPress knows which function to use
move_id: user_id,
arrow_status: 'up'
};
$.post(ajax_url, my_data, function (data) {
var objprase=$.parseJSON(data); // now obj is a json object
if (objprase.true == 'true') {
alert(objprase.true);
} else {
alert(objprase.true);
}
});
});
})(jQuery);
(function ($) {
$('.arrow-down').click(function () {
var user_id = $(this).attr('rel');
var my_data = {
action: 'arrow_handler', // This is required so WordPress knows which function to use
move_id: user_id,
dataType: 'JSON',
arrow_status: 'down'
};
$.post(ajax_url, my_data, function (data) {
var objprase=$.parseJSON(data); // now obj is a json object
if (objprase.true == 'true') {
alert("hello");
} else {
alert(objprase.true);
}
});
});
检索 Ajax Post 发送的信息并发送回加密 JSON 字符串的函数
// Finds information and sets $truetest to its value
$htmlreturn = tableReturn(); // tableReturn() gets the new HTML code
$datapasser = array("true" => $truetest, "text" => $htmlreturn);
echo json_encode($datapasser);
die(); // this is required to return a proper result
回复
<table id="blog-table" border="1">
<tr> <td align="center">UserName</td> <td align="center">User ID</td> <td align="center">Order</td> <td align="center"></td> <td align="center">Move Up/Down</td> </tr>
<tr id="1"> <td>Olesiarpm</td>
<td>18</td>
<td id="1">1</td>
<td>
<button class="del_btn" rel="1">Delete</button>
</td>
<td><div class="arrow-down" rel="18"></div> </td>
<tr id="2"> <td>bluice12</td>
<td>16</td>
<td id="2">2</td>
<td>
<button class="del_btn" rel="2">Delete</button>
</td>
<td> <div class="arrow-up" rel="16"></div><br><div class="arrow-down" rel="16"></div> </td>
<tr id="3"> <td>fSnNO4Xvlq</td>
<td>35</td>
<td id="3">3</td>
<td>
<button class="del_btn" rel="3">Delete</button>
</td>
<td> <div class="arrow-up" rel="35"></div><br><div class="arrow-down" rel="35"></div> </td>
<tr id="4"> <td>heklylh5469</td>
<td>13</td>
<td id="4">4</td>
<td>
<button class="del_btn" rel="4">Delete</button>
</td>
<td><div class="arrow-up" rel="13"></div> </td>
</table>{"true":"1","text":""} // The HTML is not shown in the "text" but instead is behind the brace