-1

当我运行我的代码时,它在 var comm_Id 上给我一个错误“ReferenceError: MyVariable is not defined”,我正在尝试获取页面 ID,然后生成 URL 以调用其余 API

这个 "${community.id}" 动态获取页面的 ID。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var comm_Id = ${community.id} //MyVariable being the supposed ID of the page
    var cat_id = ${coreNode.ancestors[0].id}; //Category ID of the boards parent
    var restUrl = "/"+comm_Id+"/restapi/vc/categories/id/"+cat_id;

    $.ajax({
        type: "GET",
        url: restUrl,
        contentType: "text/xml; charset=utf-8",
        data: "DATA",
        success: function (response) {

            var index = 0;
            var fltBoardID = "";
            var boardId="";

            var categoryNode = $(response).find("category > title");
            var categoryTitle = $(categoryNode).text();
            $("#category-title").html("<p>More in the "+categoryTitle+" category</p>");

            $(response).find("board > id").each(function () {
                boardId = $(this).text();

                var boardTitle = $(response).find("board > title:eq("+index+")");
                var otherBoardsList = $(boardTitle).text();
                $("#Other-Boards-List").append("<ul><li><a href='"+"/t5/board/bd-p/"+boardId+"'>"+otherBoardsList+"</a></li></ul>");
                index++;
            });
        },
        error: function (response) {
            $('#Other-Boards-List').html('Failed to load the content');
        }
    });
});
</script>

<div id="Other-Boards-List">
   <div id="category-title"></div>
   <p style="font-size:18px;">Browse other boards</p>    
</div>
4

1 回答 1

1

假设${community.id}在脚本运行之前被一系列字符替换,您必须将该值解释为字符串。

var comm_Id = "${community.id}";
//should result in:
var comm_Id = "MyVariable";

如果没有引号,解释器会寻找一个名为MyVariable.

正如@Michael 所指出的,您还应该引用cat_id(尽管如果它是没有前导零的整数值,则不是必需的)。

ps 也可以使用单引号代替双引号 - 唯一的区别是字符串分隔符 ( ref ) 的转义。

于 2013-10-03T07:01:43.560 回答