$.ajax post从当前页面加载HTML代码,而不是请求的PHP


$.ajax post loads html code from current page instead of requested php

我是javascript的新手,但必须使用它来加载.php

这是我的阿贾克斯.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。

我的开始.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>

在这里,我的代码有效。

但是,如果我单击指向该部分的链接.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 代码。从加载的部分复制.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-var 添加到"开始".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 的更多信息,请点击以下链接

好吧,我发现了我的错误,不敢相信。这是因为在设置 get-vars 之前我在链接中有一个斜杠

http://localhost/2play/Section.php**/**?sec=$row->descendant_id'> 

删除后,问题消失了。