前些天做前端登录跳到后端去,发现ajax提交不成功。发现自己没有考虑JS跨域问题。
对于这个问题,我的解决思路是:
iframe一个后台的页面,嵌入在前端页面上。
这样实际上提交信息就不存在跨域问题了。
提交后我们要对页面进行处理,不能还在iframe里面,后来我又做了几个测试。
demo如下:
这是iframe页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>iframe</title> </head> <body> <h1>>>>>this is a page to test iframe</h1> <iframe src="iframe_test.html"> </iframe> </body> </html>
iframe页面负责嵌套一个页面
这是iframe_test页面,被iframe页面嵌套的页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>iframe_test</title> <script type="text/javascript"> <!-- function redirect(){ //window.open('iframe_test_1.html','newwindow','height=700,width=800,top=20,left=100,toolbar=yes,menubar=yes,scrollbars=no, resizable=yes,location=yes, status=yes')
//这里是用于新建一个页面出来 parent.location.href ="iframe_test_1.html"
//这个意思就是父级页面跳到iframe_test_1.html
} //--> </script>
</head>
<body> <button onclick ="redirect()" title ="for testting page iframe">testIframe</button> </body>
</html>
其中有一个按钮,来控制做跳转。
这是iframe_test_1
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>iframe_test_1</title> </head> <body> <h1>>>>>this is a page by click button</h1> </body> </html>
用来跳转的页面