【问题标题】:javascript(jquery ajax) in php filephp文件中的javascript(jquery ajax)
【发布时间】:2015-06-29 08:00:45
【问题描述】:

我有一个 php 文件,我在其中编写了 javscript 代码。 这是代码

<html>
<head>
<script type="text/javascript"  src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript" src="jquery.cycle.all.js"></script>
<script type="text/javascript"> 
$(document).ready(function() {
alert("Start");
$.ajax({
     type:...
     url:...
     success:function(data, textStatus ){
       alert("Success");
       Jquery Cycle plugin
     }
     error:function(xhr, textStatus, errorThrown){
       alert("Failure");
     }
  });
});

我在 php 文件中有一个扩展名为 .php 的 javascript 代码。当我运行上面的代码时,php 会完美执行。但是 javascript 代码没有执行。我说这是因为即使脚本第一行中的 alert() 也没有正在执行。我没有打印出任何在 ajax 调用中定义的 ajax 错误或成功函数。

【问题讨论】:

  • 如果你把你的jQuery放在&lt;body&gt;而不是head里会发生什么。我通常在正文中工作,PHP + Javascript 组合工作得很好,但我没有在头脑中尝试过,所以不知道它是否可以工作
  • 另外,您的控制台中是否有任何错误?
  • 我在notepad++中编写代码并在浏览器中测试的控制台
  • 你关闭脚本标签了吗?
  • 浏览器控制台,有没有报错?

标签: javascript php jquery ajax


【解决方案1】:

您在 ajax 函数中遗漏了逗号 (,)。

它在我的最后工作,并进行了以下更改:

<html>
<head>
<script type="text/javascript"  src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript" src="jquery.cycle.all.js"></script>

<script type="text/javascript"> 
$(document).ready(function() {
alert("Start");
$.ajax({
 type:'GET',
 url:'somepage.php',
 success:function(data, textStatus ){
   alert("Success");

 },
 error:function(xhr, textStatus, errorThrown){
   alert("Failure");
 }
  });
});

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript"> 
$(document).ready(function() {
alert("Start");
$.ajax({
     type:'GET',
     url:'somepage.php',
     success:function(data, textStatus ){
       alert("Success");

     },
     error:function(xhr, textStatus, errorThrown){
       alert("Failure");
     }
  })
});
</script>

Also running on JS Fiddle.

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 2014-05-07
    相关资源
    最近更新 更多