Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据从JS页面发送到没有表单或URL的HTML页面_Javascript_Html_Forms - Fatal编程技术网

Javascript 将数据从JS页面发送到没有表单或URL的HTML页面

Javascript 将数据从JS页面发送到没有表单或URL的HTML页面,javascript,html,forms,Javascript,Html,Forms,我想将一个变量从JavaScript发送到另一个HTML页面并重定向到该页面,但我不能使用,因为第一个页面完全是JavaScript和.js文件,所以我不能声明表单。我也不能使用URL,因为我的数据太大了。还有什么其他选择?我找到的每个教程都使用表单或URL。还有别的选择吗 基于,我使用了以下代码: function post(array) { var method = "post"; var form = document.createElement("form");

我想将一个变量从JavaScript发送到另一个HTML页面并重定向到该页面,但我不能使用,因为第一个页面完全是JavaScript和.js文件,所以我不能声明表单。我也不能使用URL,因为我的数据太大了。还有什么其他选择?我找到的每个教程都使用表单或URL。还有别的选择吗

基于,我使用了以下代码:

function post(array) {
    var method = "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", "helloworld.html");

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "array");
    hiddenField.setAttribute("value", array);

    form.appendChild(hiddenField);

    document.body.appendChild(form);
    form.submit();
}
我用另一种方法调用post(参数)


它成功重定向到helloworld.html,但如何获取传递的变量?

您可以从JavaScript发出HTTP请求:

// jQuery
$.get("demo_test.asp", function(data, status){
        // do something
});

jQuery文档位于

您可以从JavaScript发出HTTP请求:

// jQuery
$.get("demo_test.asp", function(data, status){
        // do something
});

jQuery文档位于

您可以使用jQuery ajax API并通过POST请求发送数据。查看更多: 或


编辑:哦,我没有注意到您没有启用任何服务器端处理程序。在这种情况下,如果不想使用表单,可以使用jQuery.param()处理get/url参数,或者使用一些路由启用库

您可以使用jQuery ajax API并通过POST请求发送数据。查看更多: 或


编辑:哦,我没有注意到您没有启用任何服务器端处理程序。在这种情况下,如果不想使用表单,可以使用jQuery.param()处理get/url参数,或者使用一些路由启用库

如果第二页在同一域中,则可以将数据保存在
localStorage
中。数据必须是字符串

第一页:

function sendData(){
    localStorage.setItem("data", data);
    location.href = "helloworld.html";
}
function onLoad(){
    var data = storage.getItem("data");
}
保存数据

第二页:

function sendData(){
    localStorage.setItem("data", data);
    location.href = "helloworld.html";
}
function onLoad(){
    var data = storage.getItem("data");
}
读取数据

可选:
您还可以创建一个JSON对象,并使用函数
JSON保存它。stringify

您可以将数据保存在
localStorage
中,如果第二个页面位于同一域中。数据必须是字符串

第一页:

function sendData(){
    localStorage.setItem("data", data);
    location.href = "helloworld.html";
}
function onLoad(){
    var data = storage.getItem("data");
}
保存数据

第二页:

function sendData(){
    localStorage.setItem("data", data);
    location.href = "helloworld.html";
}
function onLoad(){
    var data = storage.getItem("data");
}
读取数据

可选:
您还可以创建一个JSON对象,并使用函数
JSON保存它。stringify

您是否已经尝试过我的示例,或者已经有了更好的解决方案?您是否已经尝试过我的示例,或者已经有了更好的解决方案?