我有一个选择菜单,我正在显示选项列表
我需要使用服务器端JavaScript来调用外部第三方数据库,并返回和数组。我有客户端JavaScript调用数组并填充选择菜单中的选项。
我不确定如何将我的阵列从服务器端传递到客户端。我很好用我的客户端Javscript和我的服务器端JavaScript,不知道如何将我的数组从服务器传递到客户端。任何输入将不胜感激。代码如下。
服务器端Javascript
var arrayOptions = new Array();
arrayOptions= //call to DB is here
for(var i = 0; i < arrayOptions.length; i++) {
var opt = arrayOptions[i];
arrayOptions.push(opt);
}
客户端JavaScript
function getOptions(arrayOptions){
var select = document.getElementById("selOption");
for(var i = 0; i < arrayOptions.length; i++) {
var opt = arrayOptions[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
答案 0 :(得分:0)
Well, passing data from server to client usually involves AJAX requests. For instance, the POST request, can be sent to the server and then the server can respond to it however you'd like. I'm not sure how you're running your server, but I get a slight feeling you might not be running a server exactly. My advice is to learn how client/server communication works first, then sending an array is as easy as sending a string or number. Suggested reading:
Node.js - serverside Javascript AJAX - Sending data to the server without reloading the page
答案 1 :(得分:0)
This function ScriptManager.RegisterStartupScript()
may helpful for you,if you are using using .NET Technologies.
Note : Hence you did not mention the technology thus I assume that,it should be .NET technologies and give my solution.