它适用于Chrome和其他所有浏览器,而不是IE,我在下面做了一个例子:
http://develop.davzy.com/test1.php
(谈论IE 8,我不确定它是否在9中运行正常)
当我运行JSON请求时,我收到一个错误,指出对象是预期的。这发生在第29行。
<!DOCTYPE html>
<html>
<head>
<title>Sandbox</title>
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
</head>
<script>
function search()
{
$.ajax({
url: 'http://davzy.com/cache/api/rarevalues.php?callback=?&search=' + $('input').val(),
cache: false,
dataType: 'json',
success: function(data, status, xhr) {
$('body').append('<br>' + data[0].name);
$('input').focus().val('');
}
});
}
</script>
<body>
<div>type test or rare or chair</div>
<input onkeydown='if(event.keyCode == 13) search();'>
<button onclick='search()'>Go</button>
</body>
</html>
以下是它返回的JSON示例(回调工作正常,忽略?这来自http://davzy.com/cache/api/rarevalues.php?callback=?&search=petal)
?([{"0":"18","id":"18","1":"Petal Patch","name":"Petal Patch","2":"75","parent":"75","3":"cache\/rare_values\/images\/petal_patch.gif","small_image":"cache\/rare_values\/images\/petal_patch.gif","4":"cache\/rare_values\/images\/large\/petal_patch.gif","big_image":"cache\/rare_values\/images\/large\/petal_patch.gif","5":"A little bit of outdoors indoors..","motto":"A little bit of outdoors indoors..","6":"0","displayorder":"0","7":"1_center cache\/rare_values\/images\/petal_patch_1.png\n1_left cache\/rare_values\/images\/petal_patch_2.png","interactiveimages":"1_center cache\/rare_values\/images\/petal_patch_1.png\n1_left cache\/rare_values\/images\/petal_patch_2.png","hcs":12.5,"throne":0.06,"credits":"25"},{"0":"685","id":"685","1":"Petals","name":"Petals","2":"28","parent":"28","3":"cache\/rare_values\/images\/petal_flurry.gif","small_image":"cache\/rare_values\/images\/petal_flurry.gif","4":"cache\/rare_values\/images\/large\/petal_flurry.gif","big_image":"cache\/rare_values\/images\/large\/petal_flurry.gif","5":"Romance is in the air. And so are rose petals apparently.","motto":"Romance is in the air. And so are rose petals apparently.","6":"0","displayorder":"0","7":"","interactiveimages":"","hcs":1.5,"throne":0.01,"credits":"3"}])
Strimp099是正确的,我需要标题('content-type:application / json; charset = utf-8');我的IE将其视为JSON。谢谢!
答案 0 :(得分:1)
Strimp099是正确的,我需要标题('content-type:application / json; charset = utf-8');我的IE将其视为JSON。谢谢!
答案 1 :(得分:-1)
这里的答案解释了所有:JavaScript KeyCode Values are "undefined" in Internet Explorer 8
请改为尝试:
var keyCode = (window.Event) ? e.which : e.keyCode;
if (keyCode == 13) {
search();
}
此外,我相信当您在输入字段上输入时,您的按钮点击可能会触发。所以你要两次调用搜索。如果你改变了怎么办?
<button onclick='search()'>Go</button>
到:
<input type="text" onclick='search()' value="Go" />
?