我试图让jQuery检索PHP变量并将它们插入到输入值中。我无处不在寻找解决方案。jQuery没有从PHP脚本接收JSON编码的数据
这是jQuery代码。
$(document).ready() {
$(function() {
$.ajax({
type: 'POST',
url: 'loadstuff.php',
data: {
'something': something,
'different': different,
'another': another
},
dataType: 'json',
success: function(data) {
$('input[name=get_seomething_here]').val('something');
$('input[name=get_different_here]').val('different');
$('input[name=get_another_here]').val('another');
}
});
});
});
而这里是PHP方面。
//connecting to db etc.
$query = "SELECT something, different, another FROM stuff WHERE id='1'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
echo(json_encode(array('something' => $row['something'],
'different' => $row['different'],
'another' => $row['another']
)));
你有没有尝试设置标题为text/json?在你的PHP回声之前,把:header('Content-type:text/json'); – kingcoyote 2012-03-14 17:48:44
@kingcoyote适用于json的'Content-type'是'application/json';所以它应该看起来像'header('Content-type:application/json');' – Edwin 2012-03-14 18:13:19
要记住的另一件事是强制'JSON_NUMBERIC_CHECK',否则所有的PHP数字都将被转换为JavaScript字符串类型。如果你正在查询的列不包含数字,你不必担心,但你应该这样做,因为这是一个好习惯。 – Edwin 2012-03-14 18:15:09