doujiao7325 2017-11-26 11:22
浏览 77
已采纳

Javascript - 未捕获的ReferenceError:未定义azZkVUpgbG,而控制台成功打印我的数据

I have the follwing JS function:

function setOptions(fullName, userID, qObjID) {
    document.getElementById("reportUserButt").innerHTML = "<a id='reportUserButt' href='javascript:reportUser(" + userID + ");'> <h5>Report @" + fullName + "<span style='opacity: 0.5'> -- " + userID + "</h5></a>";
}

which calls this func:

// REPORT A USER ---------------------------------------
function reportUser(userID) {
    $('#optionsModal').modal('hide');

    console.log("userID: " + userID);

    // Show loading modal
    document.getElementById("loadingText").innerHTML = "Reporting User, please wait...";
    $('#loadingModal').modal('show');


    $.ajax({
            url:"report-user.php?userID=" + userID, 
            type: "GET", 

            success:function(data) {
                var results = data.replace(/\s+/, ""); //remove any trailing white spaces from returned string
                console.debug(results);

                $('#loadingModal').modal('hide');

                alert("Thanks for reporting this user! We will check it out within 24h.");

                // Reload page
                setTimeout(function(){
                    location.reload();
                }, 100);

            // error
            }, error: function (e) { alert(e);}
        });
}

So I'm sure that my reportUserButt is correct because it shows me the "userID" variable in my php page, but the error I get when i click that href is:

Uncaught ReferenceError: azZkVUpgbG is not defined

azZkVUpgbG is the userID I need, so what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douju9847 2017-11-26 11:28
    关注

    Userid azZkVUpgbG is a string, when you pass it to a function:

    href='javascript:reportUser(" + userID + ")';
    

    the code becomes:

    href='javascript:reportUser(azZkVUpgbG)';
    

    So, as you can see, you call reportUser with argument azZkVUpgbG which is not string.

    So, the solution is to add quotes:

    href='javascript:reportUser(\"" + userID + "\");'
    

    Now, your code becomes

    href='javascript:reportUser("azZkVUpgbG");'
    

    and you pass a string to a function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效