目前,我的代码中遇到了一个令人沮丧的错误。看来我无法在 php 中回显 ajax 调用。下面是调用的代码。任何建议将不胜感激,谢谢。
echo'
<script>
$.get("fxn.php", { r: ""+result, id:""+'.$id.' } ).
success(function(){
alert("FXN");
});
</script>';
注意:
- 结果是一个 JavaScript 变量。
- $id 是一个 PHP 变量。
- 成功函数永远不会被调用。
最佳答案
// 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/