如何在javascript中分离ajax响应数据

如何在javascript中分离ajax响应数据,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个ajax函数,它从php页面返回一组值。 我需要得到只需要的值。我该怎么做 ajax.js function MakeRequest() { var xmlHttp = getXMLHttp(); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 4) { HandleResponse(xmlHttp.responseText); } } xmlH

我有一个ajax函数,它从php页面返回一组值。 我需要得到只需要的值。我该怎么做

ajax.js

function MakeRequest()
{
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", "ajax.php", true); 
  xmlHttp.send(null);
}
ajax.php

<?php
require_once('config.php');
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM thr';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
echo "<table border='1'>";
while($row = mysql_fetch_assoc($retval))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['ther_name'] . "</td>";
echo "<td>" . $row['region'] . "</td>";
echo "<td>" . $row['phone_no'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>";
echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>

如何获取此

的值首先,您只能从表中获取所需的值。如果我理解正确,您可以使用以下查询

  $sql = 'SELECT corrd_lattitude,corrd_longitude FROM thr';
它将只返回表中的tw0列值

 $json = json_encode(array($row['corrd_lattitude'],  $row['corrd_longitude']));
 echo $json;

在这里,lats和lngs只是一个可变名称,用于在其中存储值,并从lats和lngs VARAIBLE获取其他页面上的值。

您可以根据请求指定参数。
在获取方法中:
ajax.php?corrd\u latitude=true&corrd\u longitude=true

在php文件中,您可以在
$\u GET
数组ie中检查此项:

<?php
[...]
echo "<table border='1'>";
while($row = mysql_fetch_assoc($retval))
{
  echo "<tr>";
  if ($_GET['id'] == true) {
    echo "<td>" . $row['id'] . "</td>";
  }
  if ($_GET['ther_name'] == true) {
    echo "<td>" . $row['ther_name'] . "</td>";
  }
  if ($_GET['region'] == true) {
    echo "<td>" . $row['region'] . "</td>";
  }
  if ($_GET['phone_no'] == true) {
    echo "<td>" . $row['phone_no'] . "</td>";
  }
  if ($_GET['address'] == true) {
    echo "<td>" . $row['address'] . "</td>";
  }
  if ($_GET['corrd_lattitude'] == true) {
    echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>";
  }
  if ($_GET['corrd_longitude'] == true) {
    echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>";
  }
  echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>

在响应中使用JSON或其他替代方法是将这两个值与“-”或一些特殊字符连接起来,在响应中拆分它们。
use json:-----------
=========================================

$sql = "SELECT * FROM thr"; 
$rs = mysql_query($sql);
$res = mysql_fetch_assoc($rs);

if(mysql_num_rows($rs)>0){
echo json_encode(array('lats'=>$res['corrd_lattitude'],'lngs'=>$res['corrd_longitude']));
}else{
echo json_encode(array('lats'=>'','lngs'=''));
}
<?php
[...]
echo "<table border='1'>";
while($row = mysql_fetch_assoc($retval))
{
  echo "<tr>";
  if ($_GET['id'] == true) {
    echo "<td>" . $row['id'] . "</td>";
  }
  if ($_GET['ther_name'] == true) {
    echo "<td>" . $row['ther_name'] . "</td>";
  }
  if ($_GET['region'] == true) {
    echo "<td>" . $row['region'] . "</td>";
  }
  if ($_GET['phone_no'] == true) {
    echo "<td>" . $row['phone_no'] . "</td>";
  }
  if ($_GET['address'] == true) {
    echo "<td>" . $row['address'] . "</td>";
  }
  if ($_GET['corrd_lattitude'] == true) {
    echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>";
  }
  if ($_GET['corrd_longitude'] == true) {
    echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>";
  }
  echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>