JSONP和401响应

时间:2012-07-31 09:52:33

标签: javascript html jsonp

您好在我的网页上尝试使用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),那么我怎样才能读到我得到的回复?

1 个答案:

答案 0 :(得分:1)

你设置了#callback = updateStats&#39;。现在为你的js添加功能

function updateStats(response){
 // do something with response
}