我使用php与mysql数据库交谈,然后用json_encode编码关联数组。我知道的很多工作,我可以直接调用php并查看json结果。jQuery Json + jQuery onChange
使用jQuery我在下拉菜单上使用onChange来调用php函数并获取结果。
我认为我有jQuery和json的问题,但我已经浏览了多个帖子,找不到一个好的答案。
这是我使用的代码。我如何到达json数组?我已经尝试过数据['ele'],data.ele,data [0],所有的返回值都是未定义的。
$('#itemDD').change(function(data){
alert("Changing");
askServer(function(data){
alert("!" + data['retailListPrice']);
//spin through the data and put the results in workingQuote
});
});
function askServer(callback)
{
var selItem = $('#itemDD').val();
var strUrl = "getItemList.php?item=" + selItem + "&action=itemInfo";
alert(strUrl);
jQuery.ajax({
url:strUrl,
success:callback,
dataType: "json"
});
}
});
PHP
$sql="SELECT *
FROM items
WHERE modelNum='" . $selectedModel . "'";
$result = mysql_query($sql,$conn) or die("Error" . mysql_error());
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray));
echo json_encode($resultArray);
确保您提供的网址OK,尽量绝对URL,如“http://example.com /getItemList.php?item=“+ selItem +”&动作= itemInfo“与”http://“ – 2012-03-29 02:40:59
我给了它完整的网址,而且什么也没有。我正在更新与PHP。 – 2012-03-29 02:46:37