我的问题可能听起来很旧,已经回答了你,但我没有找到令人满意的答案.让我清楚地解释我的情景,
我有这样的index.php
的index.php
<?php
inlcude("connect.php");
include("functions.php");
?>
<html>
$(document).ready(function() {
$('#button').click(function() {
$.ajax({
type: 'GET',
data: 'method=test',
success: function(data) {
alert(data);
}
});
});
});
</html>
Run Code Online (Sandbox Code Playgroud)
我没有通过URL在Ajax调用,我得到了成功的数据警报但是从整个页面<html>来</html>.当我向functions.php提供url时,我在警告框中获得了预期的输出.
我希望你的家伙能够仔细猜测我的问题.我的问题是我需要调用一个functions.php已经包含在我的index.php页面中的函数.我不想functions.php在ajax'URL:'中再次使用,因为它functions.php会被调用两次.这是为了避免服务器负载.
让我你的家伙想出有趣的想法.
谢谢.
扩展Quentin所说的,你不能直接从Ajax请求调用php函数,但你可以将GET和POST数据传递给php脚本并执行if或switch语句来调用你的函数,这样你就可以做类似的事情了这个...
//你的Ajax电话
$.ajax(
{
type:'GET',
url:'functions.php',
data:"function_to_call=0",
success: function(data){
alert('successful');
}
});
Run Code Online (Sandbox Code Playgroud)
你的PHP函数脚本看起来像这样
switch ($_GET['function_to_call'])
{
case 0:
{
function1();
break;
}
case 1:
{
function2();
break;
}
default: break;
}
//And your functions.
function function1() {
echo "This is function 1";
}
function function2() {
echo "This is function 2";
}
Run Code Online (Sandbox Code Playgroud)