无法将 C# 字符串变量传递给 JavaScript 文件

Unable to pass C# string variable to JavaScript file

我目前正在尝试做的是将 JSON 字符串从后端 C# 传递到 JavaScript 以填充下拉列表。目前正在发生的事情是,当我使用提供的 link 中的信息时,我在 return 中得到的只是“<%= jsonFoodString %>”的文字输出。我不明白为什么会这样正在这样做。如果有人能指出我正确的方向,那就太好了。

当前post我一直在看:

Passing variable from ASP.net to JavaScript

我一直在尝试的方法是(示例):

C#代码:

    protected string jsonFoodString { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)

        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:63591/");
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.GetAsync("api/Meals/").Result;

            if (response.IsSuccessStatusCode)
            {
                string foods = response.Content.ReadAsStringAsync().Result;
                jsonFoodString = foods;
                BindData();
            }
        }
    }

Javascript:

var count = 1;
$(document).ready(function() {
$("#addItemButton").click(function() {
    if (count <= 5) {
        $("#ContentPlaceHolder1_AddMealContainer")
            .append(
                $("<div class=\"form-group\" id=\"MealItem_\"" +
                    count +
                    ">" +
                    "<div class=\"row\">" +
                    "<label ID=\"AddItemLabel_1\" class=\"col-xs-4 control-label\">Item</label>" +
                    "<div class=\"col-xs-4\">" +
                    "<select ID =\"AddItemDropdownList_1\" data-html=\"true\" data-animation=\"true\" data-toggle=\"tooltip\" data-placement=\"top\" class=\"form-control\">" +
                    "</select>" +
                    "<div class=\"has-error\">" +
                    "<span class=\"help-block\">" +
                    "</span>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>")
            );
        count++;
        var notParsedFoodString = "<%= jsonFoodString %>";
        console.log(notParsedFoodString); //Produces a literal string of "<%= jsonFoodString %>"
    } else {
        alert("You can only add 5 food items to a meal");
    }
});
$("#addItemButton").append("<input type=\"button\" value=\"Add Food Item\" id=\"AddMealItem\" class=\"btn btn-default btn-sm\">");
});

我在这里找到了我想要的答案!

var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
    if (this.readyState === this.DONE) {
        console.log(this.status) // do something; the request has completed
    }
}
xhr.open("HEAD", "http://example.com") // replace with URL of your choosing
xhr.send()