识别AJAX错误

时间:2018-01-23 17:11:47

标签: javascript php ajax svg

我有一个php文件,其中嵌入了php变量svg。在显示的svg元素上使用onclick事件(具有ID的行)我想更改SVG元素的颜色,然后将参数发送到mysql表以返回连接我选定行的其他行的x和y coords等。

这部分将参数发送到mysql表在我的javascript函数中使用ajax工作正常:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/json" />
<title>Enter Exercises</title>
</head>

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
<script type="text/javascript">

function GetPathways(elt)
{
    document.getElementById(elt.id).style.stroke = "blue";
    var test=elt.id;
    var test = document.getElementById(elt.id);
    test.setAttribute("x2", "550");
     test.setAttribute("y2", "450");       
    document.getElementById(elt.id).style.visibility = "visible";
    var exid = parseInt(elt.id);

    $.ajax(
    {                                      
      type: "POST",
      url: 'GetPathways.php',                     
      data: {id: exid},                                                      
      dataType: 'json',                  
      contentType: 'application/json; charset=utf-8',

        success: function(message)         
      {
        alert (message);
      },

        error: function(xhr, ajaxOptions, thrownError)
        {
           alert("Error code is....:"+xhr.status);
        }
    });   
};
</script>

Ajax调用php文件,该文件执行返回表行所需的sql(多个)。我已经在我的phpAdmin中测试了查询,它可以很好地检索一些适当的数据行。

然而,

Ajax脚本只执行错误功能而不执行成功功能。

成功函数最终将使用要显示的返回的svg元素的ID,即基于onclick事件更新网页。似乎很好地使用ajax来刷新页面而不重新加载。

GetPathways.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/json" />
<title>Get Pathways</title>
</head>

<body>
<?php

//echo "Hello from processing file GetPathways.php"."<br>";
header("Content-Type: application/json");
$dbname = 'TTexercises';
$dbuser = 'dummy entry';
$dbpass = 'dummy entry';
$dbhost = 'localhost:3036';


# set the database connection to be used using php function
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to   
Connect to '$dbhost'");

# select the database to be used using php function

mysql_select_db($dbname) or die("Could not open the db '$dbname'");


$id = $_POST['id'];

$query = "SELECT * FROM `PathwayTable` WHERE `EndPathwayID`= $id";

mysql_query($query);


mysql_close($conn);
?>
</body>
</html>

错误代码为200

1 个答案:

答案 0 :(得分:0)

您必须回显数据以获取来自ajax调用的响应,并且您还没有执行查询。按如下所示更改GetPathways.php

<?
header("Content-Type: application/json");
$dbname = 'TTexercises';
$dbuser = 'dummy entry';
$dbpass = 'dummy entry';
$dbhost = 'localhost:3036';


# set the database connection to be used using php function
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to   
Connect to '$dbhost'");

# select the database to be used using php function

mysql_select_db($dbname) or die("Could not open the db '$dbname'");


$id = $_POST['id'];

$query = "SELECT * FROM `PathwayTable` WHERE `EndPathwayID`= $id LIMIT 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
mysql_close($conn);
print_r($value);

?>