猿问

AJAX Jquery 调用未返回响应

您好,我正在使用 Jquery 对 Flask 服务器运行 AJAX 调用,但运行后它不会返回成功响应消息:


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

这是我提出的请求:


$.ajax({ 

    url: '/test', 

    type: 'POST', 

    data: new_freq,

    success: function(response){

        console.log(response);

    },

    error: function(error){

        console.log(error);

    }       

})

烧瓶服务器代码,


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

有人知道如何返回 AJAX 成功响应吗?


os.system()编辑:我已经替换了里面的命令ifconfig,mkdir它一切正常并得到了成功响应。该命令roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=$HOME/maps/mapname.yaml启动一个节点并且永远不会用完。那么有没有办法即使节点正在运行也可以返回成功响应?


MMMHUHU
浏览 108回答 2
2回答

慕仙森

我觉得您只想启动启动文件,而不是等到它终止。如果是这种情况,那么您需要roslaunch在后台启动该命令,即将其与正在运行的 python 进程分离: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)

饮歌长啸

你的 success 函数位置错误,试试这个。$.ajax({     url: '/test',     type: 'POST',     data: new_freq  },  success: function(response){    console.log(response);  },  error: function(error){    console.log(error);  }    })
随时随地看视频慕课网APP

相关分类

Python
我要回答