简体   繁体   English

包括asp.net填充的javascript文件

[英]include asp.net filled javascript file

I have a javascript file (marker.js) which contains a json variable : 我有一个JavaScript文件(marker.js),其中包含json变量:

var markers = [
    ['Service1', 46.81865, -71.31838367115952],
    ['Service2', 46.81248956788163, -71.29209432884048],
    ['Service3', 46.80349635182245, -71.26580498652137],
    ['Service4', 46.81248956788163, -71.39725169811672],
    ['Service5', 46.821482783940814, -71.40138736927624]
]

In the main javascript, I insert this javascript file so main function can use it. 在主javascript中,我插入了此javascript文件,以便main函数可以使用它。 => OK =>好

Now, I want to fill dynamically this var so the coordinates are dynamically get from the database and inserted in the file. 现在,我想动态填充此var,以便从数据库动态获取坐标并将其插入文件中。 Of course, this file should not be written on the disk as an other user would need the same file but with different coordinates. 当然,不应将此文件写入磁盘,因为其他用户将需要相同的文件但坐标不同。

I think I should tell the server this js file should be interpreted by the asp engine and data retrieved in the codebehind then sent to the "js interface" but how do I do that? 我想我应该告诉服务器这个js文件应该由asp引擎解释,并在后面的代码中检索数据,然后将其发送到“ js接口”,但是我该怎么做?

I would appreciate an example of both a home page and the js. 我希望看到一个同时包含主页和js的示例。

Thank you for your help! 谢谢您的帮助!

I would return JSON from the controller and include a method in the include file that accepts JSON like so: 我将从控制器返回JSON,并在包含文件中包含一个接受JSON的方法,如下所示:

function someFunction(json){
   var jsonObj = JSON.parse(json);//object is built from json returned by the controller
}

You can call this controller method using AJAX when the main view loads and pass in the JSON from the ajax call 您可以在主视图加载并通过ajax调用传递JSON时使用AJAX调用此控制器方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM