简体   繁体   中英

include asp.net filled javascript file

I have a javascript file (marker.js) which contains a json variable :

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. => OK

Now, I want to fill dynamically this var so the coordinates are dynamically get from the database and inserted in the file. 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?

I would appreciate an example of both a home page and the 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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