使用vanilla JavaScript存储JSONP请求的响应

时间:2016-10-27 23:45:46

标签: javascript jsonp

感谢您抽出宝贵时间阅读我的问题!

我正在尝试将来自JSONP请求的响应存储到一个新对象中,该对象在向同一服务器发出另一个请求后可以在以后访问该值。这样做的目的是允许用户滚动先前请求中的数据。

这是我的代码:

window.onload = getData;
function getData() {
    var script = document.createElement("script");
    script.src = "http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=displayData";
    document.getElementsByTagName("head")[0].appendChild(script);
}

function displayData(response) {
    document.getElementById("quote").innerHTML = response.quoteText;
    document.getElementById("cite").innerHTML = response.quoteAuthor;
}

我认为需要将displayData对象分配给全局变量。我已尝试过不同的方法来做到这一点,似乎无法使其发挥作用。请帮忙。

2 个答案:

答案 0 :(得分:3)

window.onload = getData;
function getData() {
    var script = document.createElement("script");
    script.src = "http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=displayData";
    document.getElementsByTagName("head")[0].appendChild(script);
}

var previousResponses = [];

function displayData(response) {
    previousResponses.push(response);
    document.getElementById("quote").innerHTML = response.quoteText;
    document.getElementById("cite").innerHTML = response.quoteAuthor;
}

现在你有一个包含所有先前回复的档案。

答案 1 :(得分:1)

使用承诺:

public static double countDistance(double lat1, double lon1, double lat2, double lon2, String unit)
{
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (unit == "K")
    {
        dist = dist * 1.609344;
    } else if (unit == "N")
    {
        dist = dist * 0.8684;
    }

    travelledDistance = travelledDistance + dist;
    return travelledDistance;
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::    This function converts decimal degrees to radians           :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg)
{
    return (deg * Math.PI / 180.0);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::    This function converts radians to decimal degrees           :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double rad2deg(double rad)
{
    return (rad * 180 / Math.PI);
}

你可以像这样使用它:

var data = new Promise(function getData(resolve, reject) {
    window.setData = resolve;
    var script = document.createElement("script");
    script.src = "http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=setData";
    script.onerror = reject;
    document.getElementsByTagName("head")[0].appendChild(script);
});

只需将window.onload = function displayData() { data.then(function(response) { document.getElementById("quote").innerHTML = response.quoteText; document.getElementById("cite").innerHTML = response.quoteAuthor; }); }; 链接到then,只要数据尚未加载,回调就会等待。