ajax request gives 500 error when called from another ajax request
NickName:Brian C Ask DateTime:2016-12-01T09:38:41

ajax request gives 500 error when called from another ajax request

I have two ajax requests, one which sends data to the backend (Let's call it Ajax#1), and then one which uses that sent data to load stuff onto the front end (Ajax#2).

In order to make sure that Ajax#2 runs after Ajax#1, I tried passing the function containing #2 as a callback to the function containing #1, where it is called after #1 finishes.

However this always seems to produce a 500 error.

Ajax #2 runs fine on it's own (when triggered by a button press), but when called from Ajax #1 it always gives a 500 error.

Any idea what I am doing wrong here?

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);

  });
};

Note that I also tried

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

Which unsurprisingly causes load to often run before apply finishes.

And I tried calling the function directly from within ajax like this:

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()
  });
};

Which gives the same 500 error.

I seem to be missing some part of how to do callbacks with asynchronous get/post requests. Any help would be much appreciated


EDIT It was requested to see the backend. I'm not sure how that would help considering that both requests work just fine in most cases. It is only when the ajax requests are nested that the outer does not work, but here they are anyway:

@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

Copyright Notice:Content Author:「Brian C」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901238/ajax-request-gives-500-error-when-called-from-another-ajax-request

More about “ajax request gives 500 error when called from another ajax request” related questions

ajax request gives 500 error when called from another ajax request

I have two ajax requests, one which sends data to the backend (Let's call it Ajax#1), and then one which uses that sent data to load stuff onto the front end (Ajax#2). In order to make sure that A...

Show Detail

Ajax Request Returns Internal Server Error

I have a drop down list which is populated with an ajax request, that is called with selecting a value from another drop down. The request is handled perfectly well every time, but when no operatio...

Show Detail

Ajax request 500 interval error

I am using Python Flask framework to build my website. I try to use ajax call to get server response and result in 500 internal error. app.py from flask import Flask, render_template,request @app...

Show Detail

Ajax Request not detecting 500 error

I'm using prototype 1.6.1. The function is designed to send a request to a server every 5 seconds while the function is turned on. It works for the most part, but every once in a while one of the

Show Detail

When does the error function in ajax request gets called

Ajax has an error function in an ajax request. When does this error function gets called. Can I know if the error function gets called if its due to an connection error.How do i get the particular ...

Show Detail

Webservice throws 500 error from AJAX, but not when called directly

I have an ASP.net application that's making a jQuery AJAX call to a VB.net webservice that lives in the same directory. On our test box, everything works beautifully. In production, if I go to the

Show Detail

500 Internal Error in ajax request

So I have this script <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript" > window.onload = function(){

Show Detail

Ajax request is not called

I am sending an ajax request on onmouseover function. This is my code <div id="notifications" class="design" onmouseover="show_drop_downn()" onmouseout="hide_drop_downn()"> &lt

Show Detail

there is no request body consumed when it called by ajax

I got a problem with ajax when I call rest API post method, I use the nodeJS to build the API side the script looks like const route = express.Router(); route.post('/tambahproduk',async(req,res)=&...

Show Detail

Laravel Ajax Request returns error 500

I am trying to create an Ajax request in laravel, but I run into 500 (Internal Server Error). Here is the route that I created: Route::post('/ajaxtest', 'PagesController@update'); This is the upd...

Show Detail