0

您好尝试在我的网页上使用 JSONP 并使用 javascript 生成以下代码:

<script src="https://{{ username }}:{{ password }}@www.link.com/api/2/project/link/resource/core/stats/?callback=updateStats&amp;random=1343727774180" id="jsonp"></script>

这是使用下面的代码生成的

function handleRefresh(url) {
    console.log("here");

    var newScriptElement = document.createElement("script");
    newScriptElement.setAttribute("src", url);
    newScriptElement.setAttribute("id", "jsonp");
    var oldScriptElement = document.getElementById("jsonp");
    var head = document.getElementsByTagName("head")[0];
    if (oldScriptElement == null) {
        head.appendChild(newScriptElement);
    }
    else {
        head.replaceChild(newScriptElement, oldScriptElement);
    }
}

function updateStats(stats) {
    var salesDiv = document.getElementById("stats");
    var newData = [];
    if(!stats){
        alert("need to log in");
    }else{
        alert("ok");
    }
    for ( var s in stats )
    {
            var data = stats[s];
            data.language = s;
            newData.push(data.language);
        }       


    for (var i = 0; i < newData.length; i++) {
        var glossa = newData[i];
        var stat = stats[glossa];

        var div = document.createElement("div");
        div.setAttribute("id", "progressbar");
        var indicator = document.createElement("label");
        indicator.setAttribute("id","indicator");

        var checkbox = document.createElement('input');
        checkbox.type = "checkbox";
        checkboxname = glossa;
        checkbox.value = "value";
        checkbox.id = "id";

        glossesArray[i]=glossa;

        div.appendChild(checkbox);
        div.appendChild(indicator);

        var zaab = parseInt(stat.completed, 10);
            //console.log(zaab);

        if(zaab < 35){
            indicator.innerHTML = glossa;
        }else{
            indicator.innerHTML = glossa + " : " + stat.completed;
            } 
        indicator.style.width = stat.completed;
        //indicator.innerHTML = glossa + " : " + stat.completed;

        if (salesDiv.childElementCount == 0) {
            salesDiv.appendChild(div);
        }
        else {
            salesDiv.insertBefore(div, salesDiv.firstChild);
        }

    }
            //alert(zaab);

    if (stats.length > 0) {
        lastReportTime = stats[stats.length-1].time;


    }
}

问题是,如果我在收到未经授权的响应(401)之前没有登录,那么我如何阅读我得到的响应?

4

1 回答 1

1

你设置了'callback=updateStats'。现在向你的 js 添加函数

function updateStats(response){
 // do something with response
} 
于 2012-07-31T10:27:21.463 回答