JavaScript code for downloading data through AJAX and then loading a chart is not functioning as expected

<script>

    var highchartsOptions = {
        chart: {
            backgroundColor: 'rgba(255, 255, 255, 0.1)',
            type: 'column'
        },
        title: {
            text: ''
        },
        exporting: {
            buttons:{
                contextButton:{
                    enabled: false
                }
            }
        },
        credits: {
                  enabled: false
            },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        colors: ['#2b908f', '#f7a35c', '#434348', '#8085e9', '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1']
    };



function showTopPickrate(data) {

    $('#containerTopPickrate').highcharts({
            chart: highchartsOptions.chart,
            title: highchartsOptions.title,
            credits: highchartsOptions.credits,
            exporting: highchartsOptions.exporting,
            tooltip: highchartsOptions.tooltip,
            plotOptions: highchartsOptions.plotOptions,
            colors: highchartsOptions.colors,
            xAxis: {
                categories: [
                    data[0]._id.championId,
                    data[2]._id.championId,
                    data[3]._id.championId,
                    data[4]._id.championId,
                    data[5]._id.championId,
                    data[6]._id.championId,
                    data[7]._id.championId,
                    data[8]._id.championId,
                    data[9]._id.championId,
                    data[10]._id.championId
                ],
                crosshair: true
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Percentage'
                }
            },
            series: [{
                name: 'Popularity',
                data: [ floor(data[0].value.pickRate*100) , 
                       floor(data[1].value.pickRate*100), 
                       floor(data[2].value.pickRate*100), 
                       floor(data[3].value.pickRate*100), 
                       floor(data[4].value.pickRate*100), 
                       floor(data[5].value.pickRate*100), 
                       floor(data[6].value.pickRate*100), 
                       floor(data[7].value.pickRate*100), 
                       floor(data[8].value.pickRate*100), 
                       floor(data[9].value.pickRate*100)]

            }, {
                name: 'Win Rate',
                data: [ floor(data[0].value.winRate*100) , 
                       floor(data[1].value.winRate*100), 
                       floor(data[2].value.winRate*100), 
                       floor(data[3].value.winRate*100), 
                       floor(data[4].value.winRate*100), 
                       floor(data[5].value.winRate*100), 
                       floor(data[6].value.winRate*100), 
                       floor(data[7].value.winRate*100), 
                       floor(data[8].value.winRate*100), 
                       floor(data[9].value.winRate*100)]

            }]
        });

}




function loadTopPickrate(day){

    $.ajax({
      url: "http://localhost:2000/mapReduce/getMapReduceResultsTopPickRate?day="day,
      type: "get",
      dataType: 'json',
      success: function(data){
            alert("got it"); // debug
            showTopPickrate(data);
      },
      error:function(){
          alert("error");
      }  
    }); 

}

$(function() {
    loadTopPickrate("all");
});

</script>

I'm finding my way in frontend web development and this code snippet is giving me a hard time. I've had experience with JavaScript before but can't figure out the issues here. Time is ticking and I need to resolve this quickly.

The script just won't run, yet I can't pinpoint where the syntax errors lie or what's wrong with it. My attempts are based on Stack Overflow solutions as I'm not well-versed in jQuery. Assistance needed in defining and utilizing functions with jQuery, or possibly debugging the AJAX call.

Answer №1

The correct way to construct the URL has not been followed

url: "http://localhost:2000/mapReduce/getMapReduceResultsTopPickRate?day="day,

This should be changed to

url: "http://localhost:2000/mapReduce/getMapReduceResultsTopPickRate?day=" + day,

Alternatively, consider using data in your AJAX request

$.ajax({
  url: "http://localhost:2000/mapReduce/getMapReduceResultsTopPickRate",
  data : {
      day : day
  },
  type: "get",
  dataType: 'json',
  success: function(data){
        alert("got it"); // debug
        showTopPickrate(data);
  },
  error:function(){
      alert("error");
  }  
}); 

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

Is there a way to determine if a button was clicked using twig?

I need assistance with implementing a button in my twig file within a table that will remove an element from an array. Ideally, I would like to remove the element based on its index. From the research I have conducted, it seems that data manipulation shou ...

Move the dist folder to the libs directory using webpack

I am interested in achieving the following task: After successfully using gulp for copying libraries, I added the below code to my tasks: gulp.task('copy:libs', function() { return gulp .src(npmdist(), { base: paths.base.node.dir }) . ...

Is there a method similar to insertBefore that works with arrays and/or HTMLCollections?

Is there a vanilla JavaScript or jQuery function that works like Node.insertBefore(), but for arrays and/or HTMLCollections? An example of what I'm looking for: var list = document.getElementsByClassName("stuff"); var nodeToMove = list[0]; var other ...

Having difficulty managing asynchronous functions with the useState hook in React

import React from "react"; import { UserContext } from "./../contexts"; import { removeStoredAuthData, storedAuthIsValid, storeNewAuthData, } from "./../utils/auth"; import { getUserInfos } from "./../api/userAuthen ...

Instructions on filling the star icon with color upon clicking the button

Currently working on a project using codeIgniter where I have a form for rating products. I am facing an issue where, upon clicking the star button, only the border color changes to yellow while the center of the button remains white. Can someone please g ...

transferring data through AJAX using the POST method

When attempting to utilize the POST method to send a variable containing a name to a server asynchronously, I encountered an error message: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data. Unfortunately, this error was n ...

The Angular component fails to retrieve data from a subscribed service when the data is being fetched from the sessionStorage

Within my Angular application, there exists a service that handles incoming objects by adding them to a list of objects, then saving the updated array to sessionStorage. This service also sends the updated list to another application that is subscribed to ...

Attempting to determine the best method for passing multiple values through an onchange event handler

Hey, I'm trying to grab the values from multiple dropdown selections for an ajax call. My goal is to get values from each individual dropdown and pass them for the ajax call. Below are three code snippets: the first two contain the values I want to co ...

React component's state is not being correctly refreshed on key events

Currently facing an issue that's puzzling me. While creating a Wordle replica, I've noticed that the state updates correctly on some occasions but not on others. I'm struggling to pinpoint the exact reason behind this discrepancy. Included ...

`AJAX jQuery for efficient file uploads`

I am struggling to upload a file input using jQuery ajax without causing the page to refresh. Here is my HTML form: <form name="uploadform" id="uploadform" method="post" enctype="multipart/form-data"> <div name="profileBiodata" id="profileBioda ...

Using D3.js to plot data points on a topojson map's coordinates

Having difficulty converting latitude and longitude coordinates to "cx" and "cy" positions on my SVG map created with d3 and topojson. Despite researching solutions online, I am unable to successfully implement the conversion process. Each time I try to co ...

"Exploring the Art of Showcasing Duplicate Image Count from User Input in an

I need to showcase multiple duplicates of 2 different images on a webpage. Users are asked for the duplication speed, which I have already implemented, and also how many copies of each image they want. function show_image() { var img = document.create ...

Tips on keeping Bootstrap Modals out of your browsing history

Imagine this scenario A visitor lands on Page A, clicks through to Page B, and then engages with a link that triggers a modal (code provided below) <a href="mycontent.html" data-target="#modal_xl" data-toggle="modal" data-backdrop="static">Click me ...

NUXT: Module node:fs not found

Encountering an error when running yarn generate in a Kubernetes container during production. The same command works fine locally and was also functioning properly in production up until last week. Error: Cannot find module 'node:fs' Require stac ...

JavaScript class syntax allows for the definition of inherited instance fields

In the code snippet below, I have implemented a way to define a prototype with a simple property that can be inherited by objects using the prototype: const SCXMLState = Object.setPrototypeOf( Object.defineProperties({ addChild() { } isStat ...

Problem with ajax functionality for users with poor internet connection

Having encountered the same issue myself here [1]: Dealing with clients behind slow connection Interestingly, the ajax call functions perfectly when utilized by fast internet users on my portal. However, it returns an empty response along with a 200 O ...

Align a collection of images in a grid format on the center of the page with

I am trying to center a div that contains multiple 145px X 145px thumbnail images without setting a fixed width. The goal is to have as many images in each row as the browser window can fit. However, I want to keep the images left-aligned within the center ...

Using a pool.query with async/await in a for-of loop for PostgreSQL

While browsing another online forum thread, I came across a discussion on using async/await with loops. I attempted to implement something similar in my code but am facing difficulties in identifying the error. The array stored in the groups variable is f ...

The process of combining identical data values within an array of objects

Can anyone help me with merging arrays in JavaScript? Here is an example array: [{ "team": team1, "groupname": "group1", "emp-data": [{ "id": 1, "name": "name1", }], }, { "team": team1, "groupname": "group1", " ...

Adjusting the timing of a scheduled meeting

Is there a way for me to update the time of a Subject within my service? I'm considering abstracting this function into a service: date: Date; setTime(hours: number, mins: number, secs: number): void { this.date.setHours(hours); this.date.s ...