javascript - $.ajax post 从当前页面加载 html 代码而不是请求的 php

标签 javascript php jquery ajax

我是 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/

相关文章:

javascript - 如何使用 SSL 将 haproxy 放在 socket.io 前面?获取混合内容警告

javascript - 在数组中查找一个项目并添加一个值

php - Carbon 'InvalidArgumentException' 覆盖 $dateFormat 属性时消息为 'Trailing data'

jQuery执行顺序问题

javascript - 将字符串保存为本地file.txt

php - 将数组从 PHP 函数返回到 jQuery

php - 跟踪给定行的 View

javascript - 将 Glyphicon 动态添加到表格行

javascript - 如何访问具有相同类名的标签并使用 javascript 处理 onClick?

javascript - 在游标上执行 forEach 函数时出现 getMore 错误