我是 JavaScript 新手,但必须使用它来 Ajax 加载 .php
这是我的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 url,如
http://localhost/Start.php?foo=bar
该脚本无法工作并会出现如上所述的一些失败。
已解决 好吧,我发现了我的错误,简直不敢相信。 这是因为在设置 get-vars http://localhost/2play/Section.php**/**?sec=$row->descendant_id'> 之前,我在链接中有一个斜杠。删除后问题消失。
最佳答案
您应该在 $.ajax
中声明变量。尝试这样的事情
$(document).ready(function(){
var data1 = "foo",
data2 = "bar";
$.ajax({
type: "POST",
url: "foobar.php",
data: {"data1": data1, "data2": data2}
});
alert(data1);
});
更多关于AJAX可以关注link
关于javascript - $.ajax post 从当前页面加载 html 代码而不是请求的 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506621/