jQuery JSON parsing issue
Need some advise on what I am doing wrong during client JSON parsing... 开发者_如何学运维 Tips and comments welcome my code returns nothing. Debugger doesn't show anything useful either.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var htmlString = "test";
$.getJSON("http://search.twitter.com/search.json?callback=functionName&q=%23csharp", functionName);
function functionName(data) {
$.each(data.items, function(i,item){
htmlString += item.content + "<br>";
});
$('#test').html(htmlString);
}
});
</script>
</head>
<body>
<div id="test"></div>
Don't specify the name of your callback function. jQuery will do that for you. The function that handles the JSONP request is specially constructed to allow you to use your function as normal and to provide other useful features.
Do callback=?
instead:
$.getJSON("http://search.twitter.com/search.json?callback=?&q=%23csharp", functionName);
The other problem is with this:
$.each(data.items, function(i,item){
You're iterating over data.items
. This doesn't exist in the response. You then ask for item.content
. This doesn't exist either. I don't know what JSON you're designing your code for, because it's not the JSON twitter sends.
You can use the $.ajax()
method instead to get more granular feedback on the status of your request.
Try dropping the callback in the url:
http://search.twitter.com/search.json?q=%23csharp
精彩评论