从控制台到PHP文件的AJAX请求不再起作用

时间:2018-11-06 00:12:24

标签: php ajax wordpress

我遇到的问题是,我手动(通过控制台)发出的用于返回数据的AJAX请求不再这样做了。与返回“真实”用户数据的当天早些时候相比,每次运行代码时,它都会产生undefined。在下面,您可以看到我发出的AJAX请求,以及将代理添加到数组然后返回它们的PHP代码。

AJAX请求

var result;

$.ajax({
      url: 'http://example.com/wp-content/alphabetise-agents.php',
      type: 'POST',
      dataType: 'JSON',
      data: { },
      success: function(response){
        result = response;

      } // end of success response
    }); // end of AJAX

alphabetise-agents.php

ini_set('display_errors', 1);
  //Connect to DB
$dbConn = mysqli_connect('localhost','wordpress','{some password}','wordpress')
or die('Error connecting to MySQL server.');

$stmt1 = $dbConn->prepare("SELECT * FROM wp_wpl_users");
$stmt1->execute();
$result1 = $stmt1->get_result();

$agents = [];

while ( $row1 = $result1->fetch_assoc() ) {


  // **** version 2 - create the html as well ****
  $first_name = $row1['first_name'];

  $last_name = $row1['last_name'];
  $last_name_for_url = str_replace(' ', '', $last_name);

  $full_name_no_space = $first_name . $last_name_for_url;
  $full_name_space = $first_name . ' ' . $last_name;

  $about = $row1['about'];
  $team_name = $row1['company_name'];
  $id = $row1['id'];

  $the_agent['html'] = '<div class="col-sm-12 col-md-3"><div class="team-block"><a class="team-block-mobile" href="/properties/' . $full_name_no_space . '" class="view_properties"></a><img src="/wp-content/uploads/WPL/users/'. $id .'/profile.png" alt="'. $full_name_space .'" /><div class="team-caption team-caption-before"><div class="team-caption-inner"><h4 class="team-name">'. $full_name_space .'</h4><p class="team-designation">'. $team_name .'</p></div></div><div class="team-caption team-caption-after"><div class="team-caption-inner"><h4 class="team-name">'. $full_name_space .'</h4><p class="team-designation">'. $team_name .'</p><p class="team-description">'. $about .'</p></div></div></div></div>';


  array_push($agents, $the_agent);


}

 $return_arr[] = array("users" => $agents);

 echo json_encode($return_arr);

查看“网络”选项卡会显示状态为200。

Wordpress可能有达到我要求的AJAX限制吗?另外,我注意到的是,更改mysqli_connect设置以尝试强制错误(使用错误的用户名/密码)实际上并没有在控制台中提供错误(当我第一次开始从事此工作时,我记得遇到connection refused错误。

任何帮助将不胜感激!

编辑:一些示例HTML在工作的当天早些时候返回给我:

<div class="col-sm-12 col-md-3"><div class="team-block"><a class="team-block-mobile" href="/properties/Ivan" class="view_properties"></a><img src="/wp-content/uploads/WPL/users/17/profile.png" alt="Ivan Rickard Liow" /><div class="team-caption team-caption-before"><div class="team-caption-inner"><h4 class="team-name">Ivan</h4><p class="team-designation">Red Sevens</p></div></div><div class="team-caption team-caption-after"><div class="team-caption-inner"><h4 class="team-name">Ivan</h4><p class="team-designation">Red Sevens</p><p class="team-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p></div></div></div></div>

0 个答案:

没有答案