1

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> 
  <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!

Kara
  • 6,115
  • 16
  • 50
  • 57
David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • What is supposed to happen? I type in a word, hit the button and the word appears below. – Jason Strimpel Jul 02 '11 at 19:05
  • It's pulling it with JSON - but it doesn't work on my Internet Explorer, I'm not sure why, there error when I hit "go" is Object Expected" – David Zorychta Jul 02 '11 at 19:06
  • 2
    That sounds like it might be a JS error. Post some code, give us line numbers, etc. Also, when returning your JSON, make sure that you set your content-type to 'application/json; charset=UTF-8'. I'm not getting an error when clicking Go. – Jason Strimpel Jul 02 '11 at 19:10
  • Sure, I've posted the code above, and the error is on line 29, Object expected – David Zorychta Jul 02 '11 at 19:15
  • I'm not familiar with jquery, but in prototype.js, $('input') would get an element by the id="input". There is input id. Try adding id="input" to your input tag. – Jason Strimpel Jul 02 '11 at 19:19
  • Interesting suggestion strimp but sadly the selector isn't the issue, it's the JSON call. – David Zorychta Jul 02 '11 at 19:22

2 Answers2

1

Strimp099 was correct, I needed header('content-type: application/json; charset=utf-8'); for my IE to treat it as JSON. Thanks!

David Zorychta
  • 13,039
  • 6
  • 45
  • 81
-1

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" />?

Community
  • 1
  • 1
Kon
  • 27,113
  • 11
  • 60
  • 86
  • Why would that cause an 'object expected' error? It would just mean that `event.keyCode` would never be equal to `13`. – user113716 Jul 02 '11 at 19:21
  • This has nothing to do with my problem. My problem is with JSON, not this. – David Zorychta Jul 02 '11 at 19:21
  • Ah well, this is before the question was updated with more details. Also, I ran this in IE7, IE8, and IE9 and I see no errors. – Kon Jul 02 '11 at 19:23