【ajax】

ajax

Closure

(function (func) {
    $.ajax({
        url: "/data/get_map",
        type: "GET",
        dataType: "json",                       //json格式
        success: function (data) {
            func(data);
        }
    });

})(function (data) {          //data:ajax返回的数据

})

Reasons for writing in this format:

mytestarea.js

var areas=[];

area.js

var areas = [
{"code":"110000","parentCode":"0","level":"1","name":"北京市","latitude":"39.929986","longitude":"116.395645"},
{"code":"110100","parentCode":"110000","level":"2","name":"市辖区","latitude":"","longitude":""},
{"code":"110101","parentCode":"110100","level":"3","name":"东城区","latitude":"40.033162","longitude":"116.239678"},
{"code":"110109","parentCode":"110100","level":"3","name":"门头沟区",]

mytestpage.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="/static/js/area.js"></script>

<script src="/static/house/mytestarea.js"></script>

</body>
</html>

At this time, the data in arae[] is empty, because the js engine is single-threaded, loaded from top to bottom, and the later loaded will overwrite the previous variable with the same variable name.

There may be many option variables in the picture code. To avoid overwriting, set the scope of the js file

 

 

 

 

Guess you like

Origin blog.csdn.net/Qmilumilu/article/details/104783155