繁体   English   中英

Echo Ajax-PHP,AJAX,Jquery

[英]Echo Ajax - PHP, AJAX, Jquery

目前,我的代码中遇到一个令人沮丧的错误。 看来我无法在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会发生什么?

使用heredoc避免大量的qouting

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

您需要确保在回显中转义“字符。如下所示

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM