将数据追加到 open.window()



所以我喜欢将从服务器收到的数据(JSON(附加到新的弹出窗口中。我尝试了其他人已经发布的内容,但没有一个真正有效。

$.ajax
    ({
        url: "/receiveData",
        type: "POST",
        contentType: "application/json; charset=UTF-8",
        data:myJSON,
        dataType: "json",
        success: handledata
    });

所以我有这个 ajax 调用和处理数据

这是我在handledata函数上所拥有的,我喜欢将其附加到打开中称为"user"的特定id.html

var newin = window.open("open.html");
$(newin.document.body).ready(function() {
    //$(newin.document.body).append("<tr>"+ "Hello"+ "<tr>")
        newin.alert("hello");
});

你以错误的方式思考http请求。
调用window.open()时,下一个窗口没有来自现有窗口的任何数据。这是一个新的 js 实例。

解决方案是将 url 中的数据作为查询参数,例如:"open.html?data="+queryStr,然后在打开.html页面上从 <body onload="/* Your ajax call here */"> 发送 http 调用。

有方法对 json 字符串进行编码:

function encode(queryStr) {
  return b.split(' ').map(i => encodeURIComponent(i)).join('+').replace(/'/g, "%27").replace(/"/g, "%22");
}

有方法可以从 url 检索查询字符串:如何在 JavaScript 中获取查询字符串值?

最新更新