make localhost / ... popup localhost:3000 /

时间:2017-03-08 02:50:02

标签: javascript popup cors localhost postmessage

我有一个本地文件 import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Part; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(name = "changedp",urlPatterns = {"changedp"}) @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB public class changedp extends HttpServlet { // database connection settings private String dbURL = "jdbc:mysql://localhost/cll"; private String dbUser = "root"; private String dbPass = "root"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream inputStream = null; // input stream of the upload file // obtains the upload file part in this multipart request Part filePart = request.getPart("dp"); if (filePart != null) { // prints out some information for debugging System.out.println(filePart.getName()); System.out.println(filePart.getSize()); System.out.println(filePart.getContentType()); // obtains input stream of the upload file inputStream = filePart.getInputStream(); } Connection conn = null; // connection to the database String message = null; // message will be sent back to client try { // connects to the database DriverManager.registerDriver(new com.mysql.jdbc.Driver()); conn = DriverManager.getConnection(dbURL, dbUser, dbPass); HttpSession session=request.getSession(); // constructs SQL statement String sql = "UPDATE student_login_tabl SET image=? where `sid` = '"+session.getAttribute("sid")+"'"; PreparedStatement statement = conn.prepareStatement(sql); if (inputStream != null) { // fetches input stream of the upload file for the blob column statement.setBlob(1, inputStream); } // sends the statement to the database server int row = statement.executeUpdate(); if (row > 0) { message = "File uploaded and saved into database"; } } catch (SQLException ex) { message = "ERROR: " + ex.getMessage(); ex.printStackTrace(); } finally { if (conn != null) { // closes the database connection try { conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } } } ,如下所示,可由a.html启动。点击https://localhost/a.html,它可以打开另一个文件Open b,然后按b.html按钮,它可以按Send发送数据:

postMessage

现在,我想打开由本地应用程序提供服务的页面<html> <body> <p onclick="openWindow()">Open b</p> <form> <input type="button" value="Send"> </form> <script> function openWindow() { var popup = window.open("https://localhost/b.html", "popup", "width=200, height=200"); // var popup = window.open("https://localhost:3000/#/new", "popup", "width=1000, height=1000"); var button = document.querySelector("form input[type=button]"); button.onclick = function(e) { e.preventDefault(); e.stopPropagation(); popup.postMessage("hi, how are you?", popup.location.href); } } </script> </body> </html> ,而不是打开https://localhost/b.html。所以我取消注释行https://localhost:3000/#/new。然后它在打开时引发错误:

var popup = window.open("https://localhost:3000/#/new", "popup", "width=1000, height=1000");

因此Uncaught DOMException: Blocked a frame with origin "https://localhost" from accessing a cross-origin frame. at HTMLInputElement.button.onclick (https://localhost/a.html:20:49) https://localhost/似乎被认为是来源

是否有人有任何解决方案或解决方法可以https://localhost:3000/打开https://localhost/a.html

编辑1:我使用的是Mac。实际上,当进入生产时,所有内容都将放在同一个域https://localhost:3000/#/new下,该域具有www.myexample.com等静态文件并运行服务器。我只想在Mac中的a.html中有一个简单的开发解决方案。

编辑2:一个限制是,我必须localhost打开a.html;我不能使用https://localhost/a.html或将其作为静态文件(即http)提供。

2 个答案:

答案 0 :(得分:0)

您已经提到过您正在使用MEAN堆栈,因此我假设您使用的是ExpressJS。

可以提供静态(用于开发):请参阅http://expressjs.com/en/starter/static-files.html

不要在localhost上使用https ...它没有什么更安全的,只需添加一个不需要的自签名证书。

对于更通用的解决方案,nginx可用于提供静态文件并将其他路径传递给后端。

答案 1 :(得分:0)

一个解决方案是在终端中打开Chrome,其中包含一些特殊参数:

open -a Google\ Chrome --args --disable-web-security --user-data-dir=/Users/SoftTimur/Desktop/user-data-dir

我还尝试了一些Chrome加载项,但这些加载项对我的测试无效。

如果我们有一个解决方案(例如,某些设置),而不必每次都从终端打开Chrome,那就太棒了......