本文介绍了你可以在jQuery中添加页眉到的getJSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都希望得到的JSON数据,Pinterest的,但发现他们目前不要让你的用户和特定的电路板,以便发现mashape,我需要获得API密钥,但使用头请求可以在此使用的getJSON做?目前的code是:

我需要通过:X-Mashape-KEY:KEY_HERE我看到它可以在AJAX进行,但不知道如何改写的getJSON作为循环到,但可以在这里看到:

  $。的getJSON(settings.apiPath + settings.username +'/板/',功能(数据){
            beforeSend:SetHeader可以,
            的console.log(数据);

            对于(VAR I = 0; I< settings.count;我++){
                VAR饲料= FALSE;
                如果(数据。数据[I]){
                    饲料=数据。数据[I]
                }

                VAR temp_data = {
                    消息:截断(饲料[信息],100),
                    网址:喂[链接]
                };

                $('#pinterestFeed UL)追加。('<李类=进与GT;+模板(temp_data)+'< /李>');
            }
            $(Pinterest的#loading。)隐藏()。
        });
 

解决方案

$ .getJSON 是一个速记

  $。阿贾克斯({
    数据类型:JSON,
    网址:网址,
    数据:数据,
    成功:成功
});
 

所以,你可以简单地直接使用它像

  $。阿贾克斯({
    beforeSend:函数(要求){
        request.setRequestHeader(X-Mashape-键,key_here');
    },
    数据类型:JSON,
    网址:settings.apiPath + settings.username +'/板/',
    成功:功能(数据){
        //你的code
    }
});
 

ive been looking to get JSON data for pinterest but notice they currently dont let you get for a user and a specific board so found mashape and i need to get the API key but using header request can this be done using getJSON? current code is:

I need to pass: "X-Mashape-Key: KEY_HERE" i see it can be done in ajax but dont know how to rewrite the getJSON as the loop to that but can see here:

http://blog.mashape.com/mashape-sample-code-executing-ajax-request-using-jquery/

$.getJSON(settings.apiPath + settings.username + '/boards/', function (data) {
            beforeSend: setHeader,
            console.log(data);

            for (var i = 0; i < settings.count; i++) {
                var feed = false;
                if(data.data[i]) {
                    feed = data.data[i];
                }

                var temp_data = {
                    message: truncate(feed["message"], 100),
                    url: feed["link"]
                };

                $('#pinterestFeed ul').append('<li class="feed">' + templating(temp_data) + '</li>');
            }
            $(".pinterest #loading").hide();  
        });
解决方案

$.getJSON is a shorthand for

$.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success
});

So you can simply use it directly like

$.ajax({
    beforeSend: function(request) {
        request.setRequestHeader("X-Mashape-Key", 'key_here');
    },
    dataType: "json",
    url: settings.apiPath + settings.username + '/boards/',
    success: function(data) {
        //Your code
    }
});

这篇关于你可以在jQuery中添加页眉到的getJSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:42