Challenges with Scope in Using AJAX Calls within an Infowindow

Could it be a scope issue? Feel free to correct me if I'm mistaken.

I've got a for loop that's placing markers on my map. Each marker has a different infowindow that loads content using ajax callbacks.

This is a simplified version of the script with the problem highlighted:

var xhr = "";
var infowindow = new google.maps.InfoWindow();
var marker, i;
var polylineCoordinates = [new google.maps.LatLng(78.782762, 17.917843),
                           new google.maps.LatLng(-0.829439, -91.112473),
                           new google.maps.LatLng(15.066156, -23.621399),
                          ]


function createHttpRequest() {
    try {   
        xhr = new XMLHttpRequest();
        return xhr;
        }
        catch (e)
        {
            //assuming IE6
            try {
            xhr = new activeXBbject("microsoft.XMLHTTP");
            return xhr;
            }
            catch (e)   {
                return alert("Unable to create an XMLHttpRequest object");
            }
        }
}



function initialize() {

    var mapOptions = {
      center: new google.maps.LatLng(78.782762,17.917843),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
        map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);
}

for (i = 0; i < polylineCoordinates.length; i++) {
    marker = new google.maps.Marker({
        position: polylineCoordinates[i],
        map: map
    });

    google.maps.event.addListener(marker, 'click', (function (marker, i) {
        return function () {
            infowindow.setContent("<div id=\"infowindow\">" + getStationInfo(infoWindowDiv) + "</div>");
            infowindow.open(map, marker);

        }
    })(marker, i));

} 

function infoWindowDiv(stationInfo) {
    var add = document.createTextNode(stationInfo);
    document.getElementById("infowindow").appendChild(add);
}


function getStationInfo(callback) {
    var xhr = createHttpRequest();
    var url = "stations.php" 
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var stationInfo = "This is a Test";
            return callback(stationInfo)
        } 

    } 
    xhr.open("GET", url, true);
    xhr.send(null);
} 

Small Edit: Functions have been moved outside of the loop

Edit 2: The ajax call isn't the issue, the URL was changed just for the code sample. The final output displays "This is a test" in the infowindow, indicating a successful callback. Additionally, note there is no responseText or responseXml involved. The returned variable is not related to the URL

The callback seems to be working fine, but there's an unwanted 'undefined' displayed above it.
The console doesn't show any errors.
Output:

undefined
This is a test

If it's functioning, why does it display as undefined?

Answer №1

Here is what's going on:

  1. When you click on the infowindow,
  2. The function getStationInfo(infoWindowDiv) is triggered, sending an AJAX request that doesn't return any useful information (it's "undefined" due to the lack of a return statement),
  3. An error will occur in the AJAX function because the URL specified as "Unnecessary at this point" won't trigger the onreadystatechange function. However, you mentioned that this isn't a problem.
  4. A JavaScript error
    Uncaught TypeError: Cannot call method 'appendChild' of null
    happens because the div with the id infowindow hasn't been added to the DOM yet.

To address this, consider adding an event listener to the infowindow so it waits for the rendering process (domready) before trying to access the div with id="infowindow."

Corrected code snippet:

var xhr = "";
var infowindow = new google.maps.InfoWindow();
var map = null;
var marker, i;
var polylineCoordinates = [new google.maps.LatLng(78.782762, 17.917843),
                           new google.maps.LatLng(-0.829439, -91.112473),
                           new google.maps.LatLng(15.066156, -23.621399)
                          ];

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(78.782762, 17.917843),
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    for (i = 0; i < polylineCoordinates.length; i++) {
        marker = new google.maps.Marker({
            position: polylineCoordinates[i],
            map: map
        });

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent("<div id=\"infowindow\" style=\"height:50px;width:200px;\"></div>");
                infowindow.open(map, marker);
                google.maps.event.addListenerOnce(infowindow,"domready", function(){
                    getStationInfo(infoWindowDiv);
                });
            })(marker, i));
    } // End loop to add markers
}

function infoWindowDiv(stationInfo) {
    var add = document.createTextNode(stationInfo);
    document.getElementById("infowindow").appendChild(add);
}

function getStationInfo(callback) {
    var stationInfo = "This is a Test";
    callback(stationInfo);
} // End of getStationInfo

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error thrown by loader.js at line 582 of internal/modules/cjs/loader.js

Encountered this error message in the console: Error : Cannot find module. The detailed error is provided below. Any suggestions on how to resolve this? internal/modules/cjs/loader.js:582 throw err; ^ Error: Cannot find module 'C:\Users ...

"Selecting elements using the nth-of-type CSS selector alongside other

Dealing with a grid layout that includes spacers between certain items, I attempted to use the :nth-of-type selector in CSS to style only the first column of items and not apply those styles to the right side. However, it seems that the CSS gets confused w ...

Monitoring Twitter bootstrap modal for scrollbar reaching the bottom

Currently, I am working with Twitter Bootstrap modal and facing a challenge in determining how to detect when the scrollbar of the modal reaches the bottom using either JavaScript or jQuery. https://i.stack.imgur.com/0VkqY.png My current approach involve ...

Using a Javascript method to access a sibling property within an object

Is there a way to access a sibling property from a method in JavaScript? This seemingly simple task has proven challenging for me. Take a look at the sample code below. let f = { a: 3, printMyBrother() { console.log(X) } }.printMyBrother f() ...

Is it possible to include three sorting states in jQuery DataTables: ASC, DESC, and NO_SORT?

When clicking on a column header in jQuery DataTables, the sorting order toggles between ascending (Down Arrow) and descending (Up Arrow). However, I am looking to customize this behavior: 1st-click ascending 2nd-click descending 3rd-click no-sorting 4th- ...

What are the steps for converting a structured text document into a SQL table?

I am currently working on a project that involves the need to save and load a structured text document, similar to MS Word, into/from a MySQL table. For instance, if given a sample document like sample.doc, the goal is to save both the content and formatt ...

The database server is not appearing on the main.js page of the client

My client's main.js file contains code that is meant to display previous form entries on the webpage, but after submitting the form, no entries appear on the HTML page. My server is running on port 7777 and the root route works in Postman, as does the ...

passing JSON data using JavaScript or jQuery

I have a JSON snippet that I need to parse using JavaScript or jQuery and convert into variables: name and meetup. Can you help me with this? Below is the JSON code: { "MYID": 1, "module": [ { "name": "Manchester", ...

Submitting to authorize.net using Jquery and AJAX

Hi everyone, I'm facing an issue and need some help figuring out why I keep encountering the following error message: "There was an error submitting the form. Please try again. Parsererror" This happens whenever I attempt to submit my form to author ...

Personalized Angular dropdown menu

Recently, I've started delving into angularJS and I'm eager to create dropdowns and tabs using both bootstrap and angular. Although there is a comprehensive angular bootstrap library available, I prefer not to use it in order to gain a deeper und ...

Utilizing CakePHP 3.0 with jQuery UI for an autocomplete feature

Seeking assistance on why the current code isn't functioning. The objective is to retrieve data from the index controller to search and obtain JSON data. No requests are being made, and there are no visible results. New to CakePHP 3.0, I am attemptin ...

Exploring the world of nested routes and AJAX in Rails 3.2

Working with Ruby on Rails 3.2, I've built a basic test blog application featuring a Post model and a Comment model. The relationship is such that a post has_many :comments while a comment belongs_to :post. My routes.rb file looks like this: resourc ...

Symfony2 and asynchronous JavaScript and XML (AJAX)

Is there a way to perform asynchronous actions in Symfony2 without having to refresh the page? I haven't been able to find any information about this in the official "Book" or "Cookbook". (The only mention I came across was 2 sentences about hinclude. ...

How to modify ID data with AngularJS ng-repeat

I am currently searching for a solution to easily modify an ID within a repeated ng-structure. This scenario involves having a customer table with unique customer IDs, which are then utilized in another table related to orders. When I retrieve data from th ...

Can someone guide me on how to retrieve data from a MUI table within a React project

Currently, I am retrieving data from a server in JSON format and looping through this data to display specific information. Everything is functioning as expected, but I'm encountering an issue with a Popover element that contains items with onclick ev ...

Uploading Files within Angular FormArray

I am facing an issue with my formArray which contains file upload inputs in each element. Whenever I upload an image in one input, it changes the values of other file inputs in different rows. https://i.stack.imgur.com/3haZW.png Current Behavior: Uploadi ...

Deciphering the '$' symbol in the Cheerio API

I'm feeling a bit confused about the significance of using the $ sign in the Node.js Cheerio API. Take for instance the snippet of code below: if(!error){ var $ = cheerio.load(html); var title, release, rating; var json = { title : "", ...

Obtaining the MasterTableView Edit Form within a Radgrid to acquire a reference to a textbox

Can anyone help me with two things, please? I am struggling to access the currently edited existing row in the Radgrid and also the index of the Edit form when attempting to add a new record to the table. function OnClientSelectedIndexChanged(sen ...

Interact with WebBrowser Control to Retrieve Document Elements Post-AJAX Request and Handle Null Exceptions

I have encountered an issue while developing an application that utilizes the WebBrowser control to display web content that dynamically updates using AJAX to load new elements. Despite my efforts, I am unable to access these newly added elements through ...

Tips for improving performance on AJAX-based websites with unreliable networks

During my recent travels, I have come across an issue with the way Ajax constructs websites. While I understand that requesting only necessary pieces of a webpage is efficient for servers, in areas with intermittent or limited signal, sites using this mode ...