gpt4 book ai didi

javascript - 从 javascript 中的 php 文件调用函数

转载 作者:行者123 更新时间:2023-11-28 18:50:59 27 4
gpt4 key购买 nike

我编写了这段代码来从javascript调用php函数,php函数只是打印它收到的参数,但是这段代码没有给出任何输出,也没有打印任何内容,我使用的是google chrome浏览器,请帮忙。

index.html文件

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
jQuery.ajax(
{
type: "POST",
url: 'save.php',
dataType: 'json',
data: {functionname:'saveUser', arguments:["username", "password"]},

success: function (obj, textstatus) {
if( !('error' in obj) ) {
alert(obj.result);
}
else {
console.log(obj.error);
}
}
});
</script>
</head>
</html>

保存.php文件

<?php

header('Content-Type: application/json');
if( $_POST['functionname'] == 'saveUser' ) {
echo json_encode(Array(
result => $_POST['arguments'][0] . ' ' . $_POST['arguments'][1]
));
}

?>

输出 enter image description here

最佳答案

有一个错字。您缺少引用数组键

试试这个

echo json_encode(Array(
'result' => $_POST['arguments'][0] . ' ' . $_POST['arguments'][1]
^ ^
));

关于javascript - 从 javascript 中的 php 文件调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com