Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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中将数据从html页面传递到另一个页面_Javascript_Ajax_Post_Local - Fatal编程技术网

在javascript中将数据从html页面传递到另一个页面

在javascript中将数据从html页面传递到另一个页面,javascript,ajax,post,local,Javascript,Ajax,Post,Local,我知道这个问题被问了很多次,但是在阅读了答案并尝试了一些解决方案之后,我的代码仍然不起作用。 以下是我的工作文件: my_app -index.html -task1 -index.html 在我的_app/index.html中,我声明了一个数组,我必须将其发送到task1/index.html testArray =[1, 2, 3]; var myUrl= "task1/index.html"; $.ajax({

我知道这个问题被问了很多次,但是在阅读了答案并尝试了一些解决方案之后,我的代码仍然不起作用。 以下是我的工作文件:

my_app
   -index.html
   -task1
       -index.html
在我的_app/index.html中,我声明了一个数组,我必须将其发送到task1/index.html

testArray =[1, 2, 3];

    var myUrl= "task1/index.html";
          $.ajax({
                type: "POST",
                url: myUrl,
                dataType: "json",
                data: JSON.stringify({images:testArray}),
                success: function (data) {
                console.log('success');
                    alert(data);
                }
            });
             window.open(myUrl); 
//this code is called when a radio button is checked
将打开指向正确url的新窗口,但不会发送testArray。 我有两个错误:

1/ Origin null is not allowed by Access-Control-Allow-Origin.
2/the variable images is not defined when I try to access it from task1/index.html
关于第一个错误,我了解到这可能是由于谷歌浏览器在本地资源使用Ajax时的限制造成的。 正如这里所建议的,我补充说

--allow-file-access-from-files
在谷歌浏览器属性。但是路径没有被接受(我有一个弹出窗口“路径不正确”)

奇怪的是,我试图用FireFox运行代码,但仍然有错误2

我的Post Ajax请求中是否缺少任何内容?是否可以使用Ajax在两个html文件之间发送数据?(我这里没有任何客户端/服务器端应用程序)


谢谢

如果需要将数据发送到即将打开的新窗口,请使用GET并在url上传递参数

但是,您可以先创建一个空窗口,然后执行ajax并使用返回内容填充新窗口

下面是一个打开窗口并用POST调用结果填充窗口的小提琴示例:

var mywindow=window.open();
testArray=[1,2,3];
var myUrl=“/echo/json/”;
$.ajax({
类型:“POST”,
url:myUrl,
数据类型:“json”,
数据:{
json:json.stringify({
图片:testArray
})
},
成功:功能(数据){
console.log('success');
//警报(数据);
mywindow.document.body.innerHTML=''+data.images+'';
}
});

我尝试了这个$.get(“task1/index.html”,testArray),然后打开了窗口。testArray[0]在task1/索引上显示未定义。html@user1499220我加了一个例子,谢谢你,克里斯。也许我访问数据的方式不对。下面是我在task1/ndex.html$(document.ready)(函数(data){console.log((data.images).length)中的操作方式;它显示“无法读取未定义的属性长度”外观,ajax调用不用于在窗口之间传递数据。如果是真的,您可以创建一个空窗口,并用从ajax调用中获得的数据填充它(如示例中所示),ajax不适用于此。它用于将数据传递给服务器端脚本,并在同一窗口上获取结果的回调。
var mywindow = window.open();
testArray = [1, 2, 3];

var myUrl = "/echo/json/";
$.ajax({
    type: "POST",
    url: myUrl,
    dataType: "json",
    data: {
        json: JSON.stringify({
            images: testArray
        })
    },
    success: function (data) {
        console.log('success');
        //alert(data);
        mywindow.document.body.innerHTML = '<pre>' + data.images + '</pre>';
    }
});