问题出在 jQuery 中,jsondata.uname
不显示任何结果。如果我在 PHP 中 echo $myarray
并在 jQuery 中使用 data
,它将以数组形式显示结果。
$(document).ready(function() {
$('button#myloginbtn').on('click', function(e) {
e.preventDefault();
var username = $('input#myusername').val();
var password = $('input#mypassword').val();
$.post('home.php', {
domylogin: 'domylogin',
username: username,
password: password
}, function(data) {
jsondata = JSON.parse(data);
$('p#auser').text(jsondata.uname); // This is not working
});
});
});
home.php:
<?php
if (isset($_POST['domylogin'])){
$myuname = $_POST['username'];
$mypword = $_POST['password'];
// temporary testing, later i will return some data from database related to these values
$myarray = array();
$myarray['uname'] = $myuname;
$myarray['pword'] = $mypword;
echo json_encode($myarray);
}
?>
最佳答案
谢谢大家的支持,我遇到了问题。我的 php 文件中有一些 html 代码,在删除“home.php”中的 html 代码后,一切都按预期工作!
关于php 数组数据在 JQuery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35310751/