首页 > 解决方案 > 在提交之前在表单中调用 ajax,显示“未捕获的 ReferenceError”

问题描述

我正在尝试从表单内部调用 ajax 函数来提取下拉列表的值。

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

这确实可以正常工作,来自 id 和 get from 的值确实在selected_data控制台上正确显示,没有任何错误html fileselected_option_data_filter_urlhtml filter_function_url

function get_subdata_filters(selected_option_data_filter_url) {

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

ajax当我在我的 js 函数中添加一个调用时select

function get_subdata_filters(selected_option_data_filter_url) {

  var selected_data = document.getElementById("selected_data").value;
  // make the user selected data into a dictionary/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(fselected_data){
            console.log(fselected_data)
          };
        });
}

Uncaught ReferenceError: get_subdata_filters is not defined我在检查时收到此错误 console

做错了什么?我该如何纠正?

标签: javascriptjqueryhtmlajaxpython-3.x

解决方案


ajax您的通话中有错误。在成功之后
删除并在您的之后添加;;console.log()

function get_subdata_filters(selected_option_data_filter_url) {

  var selected_data = document.getElementById("selected_data").value;
  // make the user selected data into a dictionary/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(fselected_data){
            console.log(fselected_data);
          }
        });
}

推荐阅读