简体   繁体   English

在AJAX成功响应中执行Javascript代码(在单独的文件中)?

[英]Execute Javascript code(in a seperate file), within an AJAX success response?

I am trying to execute a Javascript file (which puts the Google Maps API on a div) **when the AJAX success response occurs. 我尝试执行Javascript文件(将Google Maps API放在div上) **当AJAX成功响应发生时。

I have the AJAX success displaying some JSON values, but I would like to execute the Javascript file within the same success response. 我的AJAX成功显示了一些JSON值,但是我想在相同的成功响应中执行Javascript文件。

I have the following code so far in my "main.html" file: 到目前为止,我的“ main.html”文件中包含以下代码:

        <form id="display" method="post" action="PHP_Function_2.php">

        <input type="submit" class="learnButton" name="insert" value="Display Location Evidence On Map" />

    </form>

        <script>
        $(function(){
          $("#display").submit(function(event) {
            event.preventDefault();

              $.ajax({
                     type: "POST",
                     url: $(this).attr('action'),
                     data: $(this).serialize(),
                     dataType: 'json',
                     success: function(data)
                     {                 
                        //display data...
                        //("#results").html(data.name).show; 
                        $("#results").empty();

                        console.log(data);

                        $("#map-canvas").empty();

                        $("#results").append(data); 

                        $(document).on("ready",function()
                        {
                                $.getScript( "Map_2.js" );              
                        }

                   });                
          });
        });

        </script>

And the following Javascript code in: "Map_2.js" that I would like to execute on ajax success. 我想在ajax成功时执行以下Javascript代码: “ Map_2.js” Which initializes the map on "the map-canvas" div: 它将在“ map-canvas” div上初始化地图:

function initialize() {



var mapOptions = {
    zoom: 7,
    center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);  
}

google.maps.event.addDomListener(window, 'load', initialize);

I have been trying to use the getScript() function, but I am having trouble with it. 我一直在尝试使用getScript()函数,但是遇到了麻烦。 Bit of a beginner with HTML and Jquery. HTML和Jquery的入门者。

Any help on this would be much appreciated. 任何帮助,将不胜感激。

  1. Include the map2.js file in your main.html file. 在main.html文件中包括map2.js文件。
  2. Call the initialize function in the success of the ajax call. 在ajax调用成功时调用initialize函数。

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

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