When trying to submit a form, encountering an `Uncaught ReferenceError` due to calling ajax within

Attempting to trigger an ajax function within a form in order to retrieve a value for the dropdown.

mypython.py

@app.route('/new_data', methods = ['POST', 'GET'])
def new_data():
    #Filter data and return the data(data_list,filter_function_url) to myhtml.html file 

myhtml.html

<div>
<form ....method="POST" action="{{url_for('submit_new_data')}}" method="POST" enctype="multipart/form-data">
    ....
    ....
    <select id="selected_data" onchange='get_subdata_filters("{{ filter_function_url }}")'>
         <option disabled selected>Please Select Partner State</option>
          {% for f in data_list %}
          <option value="{{f}}">{{f}}</option>
          {% endfor %}
     </select>
</form>
</div>

myjsfile.js

This works properly where the value from the id selected_data in the html file and the selected_option_data_filter_url obtained from the html is displayed accurately on the console without any issues.

function get_subdata_filters(selected_option_data_filter_url) {

  var selected_data = document.getElementById("selected_data").value;
  // convert user-selected data into dictionary/json
  var new_selected_data = {
    s_data:selected_data
  };
  console.log(new_selected_data);
  console.log(selected_option_data_filter_url);
}

However, adding an ajax call inside my js function up to select results in this error:

Uncaught ReferenceError: get_subdata_filters is not defined
as shown in the console.

What am I doing wrong? How can I fix this?

Answer №1

There seems to be an issue with your ajax call.
Please remove the ; after the success function and add a ; after calling console.log()

function gatherData(selected_option_data_filter_url) {

  var selected_data = document.getElementById("selected_data").value;
  // Convert user selected data into JSON
  var new_selected_data = {
    s_data:selected_data
  };
  console.log(new_selected_data);
  console.log(selected_option_data_filter_url);
  $.ajax({
          type: "POST",
          contentType: "application/json;charset=utf-8",
          url: selected_option_data_filter_url,
          traditional: "true",
          async:false,
          timeout: 40000,
          data: JSON.stringify({new_selected_data}),
          dataType: "json",
          success: function(filtered_data){
            console.log(filtered_data);
          }
        });
}

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

Ways to update the content of a NodeList

When I execute this code in the console: document.querySelectorAll("a.pointer[title='Average']") It fetches a collection of Averages, each with displayed text on the page: <a class="pointer" title="Average" onclick="showScoretab(this)"> ...

Oops! An error occurred while trying to load the myApp module. The module 'ui.bootstrap' is missing and causing the failure

When using Firefox, I encountered the following error: SyntaxError: syntax error xml2json.js:1 SyntaxError: syntax error ui-bootstrap-tpls-0.13.0.js:1 Error: [$injector:modulerr] Failed to instantiate module myApp due to: [$injector:modulerr] Failed to in ...

Is it possible for a chrome extension to allow only specific third-party cookies and storage to be

My Chrome extension inserts an iframe from foo.com into bar.com, creating a situation where bar.com now includes a foo.com iframe. However, I have observed that this iframe encounters difficulties when attempting to write to localStorage if the user has ...

Navigate the conversation within the dialog without affecting the content below

Is it possible to have a dialog with scrollable content on top of a page that is also scrollable, but when trying to scroll inside the dialog using the mouse wheel, only the dialog body scrolls and not the page below it? What approach can be used to accom ...

Unable to fetch local file using ajax from a local HTML page

According to Same Origin Policy, the SOP should not be applied to the file:// protocol. However, I'm having trouble with my code. I am running a testing page from my local system, and both when I try to access abc.txt in the same directory as the HTML ...

Is there a way to extract only the titles from this JSON array? (using JavaScript/discord.js)

How can I extract the "title" values from this JSON array using JavaScript? I need to retrieve all the "title"s and store them in a new array like shown below: array( `hey title1`, `hey title2`, ... ) I am unsure of the number of titles we will receive, b ...

Using PHPMailer in conjunction with a textarea form and HTML

Currently, I am attempting to piece together code from a PHP tutorial that demonstrates how to create a basic PHPMailer form for sending plain text emails to a mailing list. The simplicity of the form is ideal as it will be used by only a few individuals. ...

A guide on incorporating the close button into the title bar of a jQuery pop-up window

Check out this fiddle: https://jsfiddle.net/evbvrkan/ This project is quite comprehensive, so making major changes isn't possible. However, the requirement now is to find a way to place the close button for the second pop-up (which appears when you c ...

I have noticed that there are 3 images following the same logical sequence, however only the first 2 images seem to be functioning correctly. Can you help

Update: I have found a solution that works. You can check it out here: https://codepen.io/kristianBan/pen/RwNdRMO I have a scenario with 3 images where clicking on one should give it a red outline while removing any outline from the other two. The first t ...

PrimeNG - Sticky header feature malfunctioning in the p-table

Hello there, I am currently using PrimeNG p-table which features both horizontal and vertical scrolling. My goal is to implement a sticky header for the table, so far I have attempted two methods: [scrollable]="true" scrollHeight="350px" ...

Struggling with React integration of AdminLTE3 sidebar treeview?

I have a requirement to create a React sidebar with a category 'Staff' that, when clicked, reveals three subordinate categories. Below is the code snippet: import React, { Component } from "react"; export default class Sidebar extends Componen ...

Looking to validate each input field individually using jQuery?

I am in need of validating all form inputs in the keyup function without having to write validation for each individual input. Is there a way to achieve this using methods like each();? Apologies for what may seem like a silly question. ' ...

Using jQuery to apply gradient fill to SVG elements

Is it possible to create a gradient fill in an SVG with two or three colors dynamically? Currently, filling an SVG path with just one color can be done easily. Radial gradients are an option but don't offer much flexibility as colors have to be define ...

MUI: Autocomplete received an invalid value. None of the options correspond to the value of `0`

Currently, I am utilizing the MUI autocomplete feature in conjunction with react-hook-form. I have meticulously followed the guidance provided in this insightful response. ControlledAutoComplete.jsx import { Autocomplete, TextField } from "@mui/mater ...

Share an ASP.NET Model in serialized form along with a file containing just one Jquery Ajax call in your ASP.NET MVC project

Currently, I am working on a project to create a movie library application using asp.net. While designing the admin page, I encountered a challenge with adding movies to the database. Specifically, I needed to gather details such as name, actors, and poste ...

Transforming JSON data into comma-delimited values (with thousands separators) using Angular 5 and ES6 syntax

Is there a more elegant method to convert a JSON response into comma-separated numbers for displaying currency purposes? Here is the code I have currently: let data = { "business":{ "trasactionTableData":[ { ...

What is the best way to include CSS in a CodeIgniter project?

I am facing an issue with loading a CSS file in Codeigniter. It seems like there might be something wrong with my code. Can someone help me understand how to properly load a CSS file in Codeigniter and possibly identify any errors in my current code? < ...

Transferring cookie data between requests in CrawlSpider

My current project involves scraping a bridge website to gather data from recent tournaments. I have previously asked for help on this issue here. Thanks to assistance from @alecxe, the scraper now successfully logs in while rendering JavaScript with Phant ...

jQuery Custom Validation plugin for Currency Validation

In order to create a custom validation for a text box that only accepts money values, you can use the following code: //custom validator for money fields $.validator.addMethod("money", function (value, element) { return this.optional(element) || value.m ...

What is the best way to retrieve strings from an asynchronous POST request?

I am currently working on implementing a signup function in my Angular app using a controller and a factory. However, I am facing an issue where the strings (associated with success or failure) are not being returned from the factory to the controller as e ...