我是javascript的新手,但必须将其用于.php
的ajax加载这是我的ajax.js
$(document).ready(function(){
var url = $(location).attr('href');
var uA = navigator.userAgent;
$.ajax({
type: "POST",
url: "neo4j.php",
data: {"url": url, "userAgent": uA}
});
alert(url);
});
它应该将数据发布到neo4j.php。
我的Start.php看起来像
<head><title>Start</title></head>
<?php
include("db.php");
$sql = "SELECT * FROM category_paths LIMIT 10";
$pattern = mysql_query($sql);
while($row = mysql_fetch_object($pattern)){
if ($row->descendant_id == 1) {
echo "<a href='http://localhost/2play/Start.php'>$row->descendant_id</a><br>";
}else {
echo "<a href='http://localhost/2play/Section.php/?sec=$row->descendant_id'>$row->descendant_id</a><br>";
}
}
?>
<footer><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="ajax.js"></script></footer>
我的代码在这里工作。
但是,如果我点击section.php的链接
<head><title>Section</title></head>
<?php
include("db.php");
echo "<a href='http://localhost/2play/Start.php'>1</a><br>";
if(isset($_GET["sec"])) {
$sec = $_GET["sec"];
}
$sql = "SELECT * FROM category_paths WHERE ancestor_id = $sec AND length = 1";
$pattern = mysql_query($sql);
while($row = mysql_fetch_object($pattern)){
echo "<a href='http://localhost/2play/Game.php/?game=$row->descendant_id'>$row->descendant_id</a><br>";
}
?>
<div id="js">test</div>
<footer><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
</footer>
我的ajax.js不加载js代码,而是加载当前页面的html代码。使用firebug复制已加载的section.php:
<html>
<head>
<title>Section</title>
</head>
<body>
<a href="http://localhost/2play/Start.php">1</a>
<br>
<a href="http://localhost/2play/Game.php/?game=123">123</a>
<br>
<a href="http://localhost/2play/Game.php/?game=124">124</a>
<br>
<a href="http://localhost/2play/Game.php/?game=125">125</a>
<br>
<a href="http://localhost/2play/Game.php/?game=156">156</a>
<br>
<a href="http://localhost/2play/Game.php/?game=197">197</a>
<br>
<footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="ajax.js" type="text/javascript">
<--Here the script should be loaded, but it relaods the raw non-php code from section.php-->
<head><title>Section</title></head>
<a href='http://localhost/2play/Start.php'>1</a><br>
<footer><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
</footer>
<--End-->
</script>
</footer>
</body>
</html>
我已经发现了什么: 如果我手动将一些get-vars添加到Start.php网址,如
http://localhost/Start.php?foo=bar
脚本不会工作,并且如上所述做了一些失败。
解决 好的,我发现了自己的错误,无法相信。 这是因为我在设置get-vars之前在链接中有一个斜杠http://localhost/2play/Section.php**/**?sec = $ row-&gt; descendant_id&#39;&gt; 。删除后,问题就消失了。
答案 0 :(得分:1)
你应该在$.ajax
内点头声明变量。试试这样的事情
$(document).ready(function(){
var data1 = "foo",
data2 = "bar";
$.ajax({
type: "POST",
url: "foobar.php",
data: {"data1": data1, "data2": data2}
});
alert(data1);
});
关于AJAX的更多信息可以遵循link
答案 1 :(得分:1)
好的,我发现了自己的错误,无法相信。这是因为我在设置get-vars之前在链接中有一个斜杠
http://localhost/2play/Section.php**/**?sec=$row->descendant_id'>
删除后,问题就消失了。