【问题标题】:how to fix "500 internal server error" in response to ajax call如何修复“500 内部服务器错误”以响应 ajax 调用
【发布时间】:2019-08-15 01:36:30
【问题描述】:

我正在尝试通过 ajax 调用从表中获取数据。但我收到“500 内部服务器错误”的响应。

ajax调用脚本如下

jQuery(文档).ready(函数(){ jQuery("#btn_add_section").on("点击", function( event ) { jQuery.ajax({ 网址:'', 类型:'发布', 数据: { 行动:'getsections', }, 数据类型:'json', 成功:函数(响应){ 警报(响应); 调试器; } }); event.preventDefault(); }); });

functions.php中要调用的方法如下

function getsections() {

$output = array();
$query = "SELECT * FROM wp_sections WHERE sec_status = 1";
$result = $wpdb->get_results($query);
if(!empty($result)){
foreach($result as $row) {
array_push($output, array('sec_id'=>$row->sec_id, 'sec_title'=>$row->sec_title));
}
}

wp_send_json($output);
die;
}

add_action('wp_ajax_nopriv_getsections', 'getsections'); add_action('wp_ajax_getsections', 'getsections');

【问题讨论】:

    标签: ajax wordpress


    【解决方案1】:

    JavaScript 不介意尾随逗号,而 JSON 可以:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas

    尝试删除结尾的逗号

    jQuery.ajax({ url: '', type: 'post', data: { action: 'getsections', }

    所以变成了

    jQuery.ajax({ url: '', type: 'post', data: { action: 'getsections'}

    可能是模型状态与此无效,因此服务器无法处理请求。

    【讨论】:

    • 朋友,我已经尝试过你的建议,但得到了相同的结果。
    【解决方案2】:

    “die”是一个函数,因此应该用括号die();调用

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多