php - Echo Ajax - PHP、AJAX、Jquery

标签 php javascript jquery ajax

目前,我的代码中遇到了一个令人沮丧的错误。看来我无法在 php 中回显 ajax 调用。下面是调用的代码。任何建议将不胜感激,谢谢。

echo'
<script>
$.get("fxn.php", { r: ""+result, id:""+'.$id.' } ).
    success(function(){ 
       alert("FXN"); 
    });
</script>';

注意:

  1. 结果是一个 JavaScript 变量。
  2. $id 是一个 PHP 变量。
  3. 成功函数永远不会被调用。

最佳答案

// For those who come after, put a space after echo.
echo 
     // put type into your script tag. Be kind to the older browsers.
     '<script type="text/javascript">'.
     // are you sure jQuery has loaded at this point? Does `$` reference jQuery
     // or is there another framework that's nastying up $? (Liferay? Prototype?)
     '$.get("fxn.php", '.
     // I always prefer explicit casting, but is this defined?
     '{ r: ""+result, id:""+'
     // is $id a string? Then you need to quote it.
     .$id.' } ).'.
     // not necessary, but good practice, use the third parameter of `$.get`
     // instead of defining it externally.
     'success(function() { alert("FXN"); });</script>';

建议的替代方案:

echo '<script type="text/javascript">
         $.get("fxn.php", { r: String(result), id:"'.$id.'" }, function() { 
            alert("FXN"); 
         });
     </script>';

哦,您确定 PHP 正在返回任何内容吗?如果您在 fxn.php 上手动使用 get 会发生什么?

关于php - Echo Ajax - PHP、AJAX、Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6975965/

相关文章:

php - 函数 SQL SUM 查询

javascript onmouseover/onmouseout 保留在上一个内容中,直到悬停/触发下一个内容

jquery - 处理触摸设备和悬停效果

javascript - JQuery:对话框不会触发自动完成

Javascript 获取每一张幻灯片的模数?

php - 单选按钮值未传递给 php 函数

php - 如何在 php 中动态组合图像和文本

php - 将PHP代码重写为Python(POST请求)

javascript - 如何从数组中获取javascript中的列表

JavaScript/jQuery多维数组(对象)转php