鱼C论坛

 找回密码
 立即注册
查看: 2799|回复: 4

[已解决]无法实现重定向,各位帮我看看 应该修改前端代码 还是后台代码

[复制链接]
发表于 2020-2-20 17:22:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wp231957 于 2020-2-20 17:28 编辑

如图:  点击完登录,前端  后端   都不报错,只不过把我准备定向的url  给发送到response里去了
最佳答案
2020-2-20 19:08:33

后台返回的 html 代码片段,需要插入 html 标签中,而返回的整个 html 文档,则需要重写整个页面。
66.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-20 18:13:47 | 显示全部楼层
前端发请求的代码,后端服务代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 18:23:03 | 显示全部楼层
不二如是 发表于 2020-2-20 18:13
前端发请求的代码,后端服务代码

前端代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>wp231957个人blog登录页面</title>
  6.         <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script>
  7.         <style>
  8.       label { float: left; width: 10em; margin-right: 1em; text-align: right; }
  9.           #logtit {margin-left:180px;}
  10.           #logclick {margin-left:180px;}
  11.           #logout {margin-left:30px;}
  12.           div{ border:1px solid blue}
  13.           #divlogin {margin-top:250px ;width:500px;margin-left:400px;height:180px}
  14.     </style>
  15. </head>
  16. <body>
  17. <div id ="divlogin">
  18.     <h3 id="logtit" >用户登录界面</h3>
  19.    
  20.     <form method="POST">
  21.          <label>用户名:</label><input type="text" name="username" id="usr" value=""><br>
  22.              <label>密&nbsp&nbsp&nbsp码:</label><input type="password" id="pwd" name="password" value=""><br><br>
  23.          <input id="logclick" type="button" value="登录">
  24.          <input id="logout" type="button" value="退出">
  25.     </form>
  26.   
  27. </div>
  28. <script>
  29.     onload=function(){
  30.         document.getElementsByName("username")[0].focus()
  31.           
  32.         $('body').on('click' , '#logclick' , function()
  33.                 {
  34.             var username = $("#usr").val();  
  35.             var pwd = $("#pwd").val();  
  36.                     $.ajax
  37.                         ({  
  38.                 type: "post",  
  39.                 url: "/session",  
  40.                                 data: JSON.stringify({'usr':username,'pwd':pwd}),
  41.                                 contentType: 'application/json;charset=utf-8',
  42.                                 dataType : "json",
  43.                 success:function(hdata)
  44.                             {  
  45.                     if(hdata)
  46.                                     {  
  47.                               console.log(hdata);
  48.                     }  
  49.                                         else
  50.                                         {
  51.                                              console.log("失败了");
  52.                                         }
  53.                 }
  54.             });                       
  55.           });     
  56.    }
  57. </script>
  58. </body>
  59. </html>
复制代码


后台:

  1. @app.route('/session',methods=['GET','POST'])
  2. def get():
  3.     data = json.loads(request.get_data(as_text=True))
  4.     print(data)    #这里能正常打印前端传入的用户名密码
  5.     username=data["usr"]
  6.     pwd=data["pwd"]
  7.     if not session.get('username'):
  8.        session["username"]=username
  9.        session.permanent = True
  10.     return render_template("lybmain.html",username=username)   
复制代码

这个lybmain.html 就是想要重定向的url
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 19:08:33 | 显示全部楼层    本楼为最佳答案   

后台返回的 html 代码片段,需要插入 html 标签中,而返回的整个 html 文档,则需要重写整个页面。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 20:38:18 | 显示全部楼层
不二如是 发表于 2020-2-20 19:08
后台返回的 html 代码片段,需要插入 html 标签中,而返回的整个 html 文档,则需要重写整个页面。
  1.         $('body').on('click' , '#logclick' , function()
  2.                 {
  3.             var username = $("#usr").val();  
  4.             var pwd = $("#pwd").val();  
  5.                     $.ajax
  6.                         ({  
  7.                 type: "post",  
  8.                 url: "/session",  
  9.                                 data: JSON.stringify({'usr':username,'pwd':pwd}),
  10.                                 contentType: 'application/html;charset=utf-8',
  11.                                 dataType : "html",
  12.                 success:function(hdata)
  13.                             {  
  14.                     alert(hdata);
  15.                 }
  16.             });                       
  17.           });     
复制代码


这里的  alert(hdata);  就是后台传过来的 想要重定向的html的源码内容
后台的指令是直接在浏览器里渲染的,但是不知道什么原因,就是不显示呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-5 11:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表