Search code examples
javascriptpythonjqueryflaskros

AJAX Jquery call not returning response


Hi I am running an AJAX call to a Flask server using Jquery but it is not returning success response message once i run :

os.system("roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=$HOME/maps/"+mapname+".yaml")

Here is the request I am making:

$.ajax({ 
    url: '/test', 
    type: 'POST', 
    data: new_freq,
    success: function(response){
        console.log(response);
    },
    error: function(error){
        console.log(error);
    }       
})

flask server code,

    @app.route("/test" , methods=['POST'])
    def test():
        mapname = request.get_data().decode('utf-8')
        os.system("roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=$HOME/maps/"+mapname+".yaml")
   
        return(mapname)

anybody knows how to return the AJAX success response?

EDIT:i have replaced the command inside os.system() with ifconfig and mkdir it all works and got the success response.This command roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=$HOME/maps/mapname.yaml initiate a node and never run out.So is there a way i could return the success response even the node is running?


Solution

  • I get the sense that you just want to start the launch file, but not wait until it is terminated. If that's the case then you want to start the roslaunch command in the background, i.e., detach it from your running python process:

    import subprocess
    
    @app.route("/test" , methods=['POST'])
    def test():
        mapname = request.get_data().decode('utf-8')
        subprocess.Popen(["roslaunch", "turtlebot3_navigation", "turtlebot3_navigation.launch", "map_file:=$HOME/maps/"+mapname+".yaml"])
        return(mapname)