三生石@ 2017-07-20 13:13 采纳率: 100%
浏览 18

WordPress AJAX问题

I've been trying to send an array from PHP to javascript so I can display search results from a database. I have converted the array into JSON and now can't seem to retrieve it. I've never used AJAX but I have some people that work here that have and no one has been able to figure out the exact problem (granted not a lot of their time has been spent on it)

 $(document).ready( function() {
     var data = {};
     $.ajax({
         type: 'GET',
         url: '../wp-content/plugins/advantage-geo/get_data.php',
         data: "data",
         dataType: 'json',
         cache: false,
         success: function(result) {
            alert(result.d);
            console.log(result);
         },
         error: function(XMLHttpRequest, textStatus, errorThrown) { 
         alert("Status: " + textStatus); alert("Error: " + errorThrown); 
      }    
   });
});

That is the call from the JS file. (It closes with }); but it wasn't being taken into the code block so I removed it.)

The following is the PHP

<?php 

header("Content-type:json");

global $wpdb;

$sql = "SELECT * FROM wptest_geo_db";
$result = $wpdb->get_results($sql) or die(mysql_error());


echo json_encode($result);

print_r(json_decode($person_array));
print_r($person_array);

?>

The JSON is created properly and I can display that on the page from the php file. The AJAX call is throwing an 500 Internal Server Error and I don't know why.

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2017-07-20 13:25
    关注

    My answer may not directly talk about the 500 Server Error but going through my answer might help you fix that.

    AJAX call doesn't need you to send the HTTP request type as JSON so you just need to output the encoded JSON as string. AJAX will identify it as JSON data, you even don't need to tell jQuery AJAX that you're expecting JSON data, it'll identify.

    <?php 
        // it's not required that the response HTTP request is of type JOSN
        // just ignore this
        //header("Content-type:json");
    
        global $wpdb;
    
        $sql = "SELECT * FROM wptest_geo_db";
        $result = $wpdb->get_results($sql) or die(mysql_error());
    
        echo json_encode($result);
    
        // you've echo-ed the encoded JSON just above
        // why would you print the arrays?
        // I assume it's for testing purpose
        //print_r(json_decode($person_array));
        //print_r($person_array);
    ?>
    

    Now, onto WordPress AJAX. You're doing it totally incorrect. Please look at this document to see how to handle AJAX in WordPress https://codex.wordpress.org/AJAX_in_Plugins. Test and trial what's in the linked AJAX tutorial/document and come back here if you face any issues while trying so.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题