Why wont this JSON work in IE
It works on Chrome and every other browser, just not IE, I've made an example below:
http://develop.davzy.com/test1.php
(talking about IE 8, I'm not sure if it runs okay in 9)
When I run the JSON request I get an error that says Object expected. THis occurs on line 29.
<!DOCTYPE html>
<html>
开发者_如何学Go <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>
And here is an example of the JSON it returns (the callback is working fine, ignore the ? this is from 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 was correct, I needed header('content-type: application/json; charset=utf-8'); for my IE to treat it as JSON. Thanks!
Strimp099 was correct, I needed header('content-type: application/json; charset=utf-8'); for my IE to treat it as JSON. Thanks!
Answer here explains it all: JavaScript KeyCode Values are "undefined" in Internet Explorer 8
Try this instead:
var keyCode = (window.Event) ? e.which : e.keyCode;
if (keyCode == 13) {
search();
}
Also, I believe your button click may be triggering when hitting enter on the input field. So you're calling search twice. What if you change:
<button onclick='search()'>Go</button>
to:
<input type="text" onclick='search()' value="Go" />
?
精彩评论