在客户端(js)我使用此脚本
jQuery.ajax({
type: "GET",
url: "editold.php",
data: "call=generateCode",
success: function (response) {
alert("Succesful getting php file");
},
error: function (response) {
alert("Error getting php file");
}
});
在服务器端(php)我使用此代码:
<?php
if($_SERVER['REQUEST_METHOD']=="GET") {
echo '<script type="text/javascript">' . 'alert(" ' . 'hey' . '");' . '</script>';
$function = $_GET['call'];
if(function_exists($function)) {
call_user_func($function);
}
else {
echo 'Function Not Exists!!';
}
}
function generateCode() {
echo '<script type="text/javascript">' . 'alert(" ' . 'hey' . '");' . '</script>';
}
?>
虽然我确实收到一条警报,说我的成功功能警报意味着该文件 已成功加载,我似乎没有让最终的生成代码功能正常工作。(我没有收到第二个警报。)
我尝试了很多方法,但似乎没有任何效果。
我该怎么办?..
最佳答案
这应该可以做到:
jQuery.ajax({
type: "GET",
url: "editold.php",
data: {call: "generateCode"}, // <-- This one is changed into an object
success: function (response) {
alert("Succesful getting php file");
$('body').append(response); // <-- Append the ajax response to the page body
},
error: function (response) {
alert("Error getting php file");
}
});
如果没有,请尝试在 PHP 文件中执行 var_dump($_GET);
操作。
关于javascript - 使用ajax调用php函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18949714/