$ .getJSON请求的行为方式我不清楚.不确定如何使用callback =格式化请求? [英] $.getJSON request is behaving in a way I am unclear about. Not sure how to format the request with callback=?

查看:126
本文介绍了$ .getJSON请求的行为方式我不清楚.不确定如何使用callback =格式化请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在三个jquery json请求中,其中一个正在发出跨域错误,因为我不知道如何包括callback =? (或实际上为什么这表示json vs jsonp).

Of three jquery json requests, one of them is giving cross-domain errors because I don't know how to include the callback=? (or really why that denotes json vs jsonp).

对同一个api的两个请求,只有一个使用参数,这对我来说是新的,为什么它不起作用(我尝试添加& callback =?和其他类似的解决方案.)

Two requests to the same api, just one uses parameters and it's new to me on why it's not working (i've tried adding the &callback=? and other such solutions.)

谢谢!

http://jsfiddle.net/hCWwT/10/

var names = ["athenelive", "riotgames", "aphromoo"];

var obj = jQuery.parseJSON('{"name": {"life": "{life}","logo": "{logo}","status": "{status}","preview": "{preview}","url": "{url}"}}');

wtfJSON();

function wtfJSON() {

for (index = 0; index < names.length; ++index) {

    $.getJSON("https://api.twitch.tv/kraken/channels/" + names[index] + "/?callback=?", function (json) {

        $('body').append("Stufffff: " + obj.name.logo + "<br>");

        $('body').append("Name: " + json.name + "<br>");
        $('body').append("Logo: " + json.logo + "<br>");
        $('body').append("Status(title): " + json.status + "<br>");
        $('body').append("URL: " + json.url + "<br>");

    });

    $.getJSON("https://api.twitch.tv/kraken/streams/" + names[index] + "/?callback=?", function (json) {
        if (json.stream !== null) {

            $('body').append("Preview: " + json.stream.preview.medium + "<br>");

        }
    });

    $.getJSON("https://api.twitch.tv/kraken/channels/" + names[index] + "/videos?limit=3&broadcasts=true$callback=?", function (json) {

        $('body').append("Video Name: " + json + "<br>");

    });
}
}

推荐答案

查看控制台:

XMLHttpRequest无法加载 https://api.twitch .tv/kraken/channels/athenelive/videos?limit = 3& broadcasts = true . Access-Control-Allow-Origin不允许使用来源 http://fiddle.jshell.net .

XMLHttpRequest cannot load https://api.twitch.tv/kraken/channels/athenelive/videos?limit=3&broadcasts=true. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

$.getJSONcallback参数一起使用实际上会发送JSONP请求,而jQuery通过创建<script>标签并使用唯一的全局回调来实现.如果没有它,您将尝试向该其他网站发送常规AJAX请求,而这是您的浏览器所不允许的.

Using $.getJSON with a callback argument actually sends a JSONP request, which jQuery implements by just creating a <script> tag and using a unique global callback. Without it, you attempt to send a regular AJAX request to that other website, which isn't allowed by your browser.

添加一个callback参数,它将起作用:

Add a callback parameter and it'll work:

$.getJSON("https://api.twitch.tv/kraken/channels/" + names[index] + "/videos?limit=3&broadcasts=true&callback=?", function (json) {

这篇关于$ .getJSON请求的行为方式我不清楚.不确定如何使用callback =格式化请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆