您能给出一个用jQuery读取JSONP请求的非常简单的例子吗?我就是不能让它起作用.

推荐答案

下面是一个工作示例:

<html><head><title>Twitter 2.0</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head><body>
<div id='tweet-list'></div>
<script type="text/javascript">
$(document).ready(function() {
    var url =  "http://api.twitter.com/1/statuses/user_timeline/codinghorror.json";
    $.getJSON(url + "?callback=?", null, function(tweets) {
        for(i in tweets) {
            tweet = tweets[i];
            $("#tweet-list").append(tweet.text + "<hr />");
        }
    });
});
</script>
</body></html>

Notice the ?callback=? at the end of the requested URL. That indicates to the getJSON function that we want to use JSONP. Remove it and a vanilla JSON request will be used. Which will fail due to the same origin policy.

You can find more information and examples on the JQuery site: http://api.jquery.com/jQuery.getJSON/

Jquery相关问答推荐

JQuery AJAX 成功事件未触发

使用带有 bootstrap 验证的 Ajax 函数时出现问题

5 秒后关闭 jQuery 弹出模式

与其他 Javascript 框架相比,为什么 jQuery 被如此广泛地采用?

bootstrap 模式中的自动完成问题

未捕获的类型错误:无法读取未定义的属性toLowerCase

javascript:无效(0); vs 返回 false vs preventDefault()

如何在 jQuery 中获取浏览器滚动位置?

如何使用 jQuery 将分钟转换为小时/分钟并添加各种时间值?

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

jQuery Select 一个具有某个类的div,它没有另一个类

jQuery中追加的相反

将参数附加到 URL 而不刷新

JQuery Ajax Post 导致 500 内部服务器错误

使用 Jquery 更改页面标题

JQuery - 按值查找单选按钮

如何使用jQuery触发类更改事件?

jQuery:如何找到父母的特定子元素?

如何禁用由子元素触发的 mouseout 事件?

textarea 的 val() 与 text()