从另一个ajax请求调用时,ajax请求给出500错误

时间:2016-12-01 01:38:42

标签: javascript jquery ajax jquery-callback

我有两个ajax请求,一个将数据发送到后端(让我们称之为Ajax#1),然后一个使用该发送数据将内容加载到前端(Ajax#2)。

为了确保Ajax#2在Ajax#1之后运行,我尝试将包含#2的函数作为回调传递给包含#1的函数,在#1结束后调用它。

然而,这总是会产生500错误。

Ajax#2可以自行运行(按下按钮时触发),但是当从Ajax#1调用时,它总是会出现500错误。

知道我在这里做错了吗?

var initialize = function(){

  //Load settings config on page load (this works fine)
  load_settings_config();

  //Should apply settings on button press, and load those new settings to front end
  $("#apply-sysconfig-button").click(function(){
    apply_settings_config(load_settings_config);
  });

  //manual load settings button (This works fine)
  $("#load-settings-button").click(function(){
    load_settings_config();
  });

};

apply_settings_config = function(callback){

  //Not shown - Grabs user input from html input and select elements and loads into the variable 'settings' to be sent to back end
  //Ajax#1
  $.ajax({
      method: "POST",
      contentType: 'application/json',
      url: "/applynewsettings",
      data: JSON.stringify(settings)
  })
  .done(function(sysconfig_err_msg){
    //prompt user with error message if user input is invalid
    $("#static-ip-input-err").html(sysconfig_err_msg);

    //This does not work - gives 500 error
    callback()
  });
};

load_settings_config = function(){
  //loads the new/updated settings back to front end
  //Ajax#2
  $.ajax({
    method: "GET",
    contentType: 'application/json',
    url: "/loadinterfaceinfo"
  })
    .done(function(interface_info_new){
        interface_info = JSON.parse(interface_info_new);
        interface_selected = $("#interface-dropdown option:selected").text()
        populate_interface_info(interface_selected);

  });
};

请注意,我也试过

 $("#apply-sysconfig-button").click(function(){
    apply_settings_config();
    load_settings_config();
  });

不出所料,在申请完成之前经常会导致负载。

我尝试直接从ajax中调用函数,如下所示:

apply_settings_config = function(){

  //Not shown - Grabs user input from html input and select elements and loads into the variable 'settings' to be sent to back end
   //Ajax#1
  $.ajax({
      method: "POST",
      contentType: 'application/json',
      url: "/applynewsettings",
      data: JSON.stringify(settings)
  })
  .done(function(sysconfig_err_msg){
    //prompt user with error message if user input is invalid
    $("#static-ip-input-err").html(sysconfig_err_msg);

    //This does not work - gives 500 error
    load_settings_config()
  });
};

它给出了相同的500错误。

我似乎错过了如何使用异步get / post请求进行回调的部分内容。任何帮助将不胜感激

修改 有人要求看到后端。我不确定如果在大多数情况下这两个请求都能正常工作,那将有多大帮助。只有当ajax请求嵌套时外部不起作用,但无论如何它们都在这里:

@app.route("/applynewsettings", methods=['POST'])
def apply_new_settings():
    global sys_settings
    sia_err_msg = ""

    if request.method == 'POST':
        sys_settings = request.json

    sys_settings = convertToASCII(sys_settings)

    mtu = sys_settings["mtu"]
    ip_address = sys_settings["ip_address"]
    subnet_mask = sys_settings["subnet_mask"]
    gateway = sys_settings["gateway"]
    interface = sys_settings["interface"]

    #Not shown - validation and build string to send command

    if "sia" in sia_cmd:
        cli.sendCmd(sia_cmd)
        sia_err_msg = "Sucess!"
        print "SEND CMD: "+sia_cmd
    else:
        print "sia cmd was invalid: "+"'"+sia_cmd+"'"
        sia_err_msg = sia_err_msg + "Apply Settings Failed!"

    return sia_err_msg

#Sends cmd through telnet cli, parses response, returns to front end
@app.route("/loadinterfaceinfo")
def load_interface_info():
    #Sends cmd through telnet cli & parses response
    interface_dict = run_cmd(cli,"get_interface_info")
    #must encode as json to send to front end
    interface_dict = json.JSONEncoder().encode(interface_dict)
    #send to front end
    return interface_dict

1 个答案:

答案 0 :(得分:0)

问题的根源在后端。每个请求都使用telnet发送命令。当两个命令以快速顺序运行时(另一个中有一个ajax请求),第二个telnet命令将挂起,因为第一个命令尚未完成。为什么这导致500错误(这让我相信第二个ajax请求根本没有与后端通信)我不太确定。

快速(和草率)修复是添加超时以使第二个ajax在运行之前等待一秒。

像这样:

  $.ajax({
      method: "POST",
      contentType: 'application/json',
      url: "/applynewsettings",
      data: JSON.stringify(settings)
  })
  .done(function(sysconfig_err_msg){
    $("#static-ip-input-err").html(sysconfig_err_msg);

    setTimeout(function(){ load_settings_config(); }, 1000);

  });

更好(也更困难)的解决方案是确保在后端识别出正在运行的另一个命令正在使用telnet,等待它完成,然后运行第二个命令并返回数据。 / p>

不知道如何做到这一点。欢迎提出意见。