将数据从html页面传递到javascript中的另一个页面 [英] passing data from an html page to another in javascript

查看:837
本文介绍了将数据从html页面传递到javascript中的另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多遍了,但是在阅读了答案并尝试了一些解决方案后,我的代码仍然无法正常工作. 这是我的工作文件:

I know this question was asked many times, but after reading the answers, and trying some solutions, my code still doesn't work. Here are my working files :

my_app
   -index.html
   -task1
       -index.html

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

In my_app/index.html, I declare an array , that I have to send to 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. 我有2个错误:

The new window which points to the right url opens, but the testArray isn't sent. I got 2 errors:

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

对于第一个错误,我读到这可能是由于Google Chrome在将Ajax与本地资源一起使用时的限制所致. 如此处建议访问控制不允许原始null -Allow-Origin ,我添加了

For the 1st error, I read that this may be caused by Google Chrome's restriction when it comes to use Ajax with local resources. As suggested here Origin null is not allowed by Access-Control-Allow-Origin, I added

--allow-file-access-from-files

在Google Chrome浏览器属性中.但是路径未被接受(我有一个弹出窗口路径不正确").

in Google Chrome properties. But the path was not accepted (I had a pop-up window "path not correct").

奇怪的是,我尝试使用FireFox运行代码,但仍然出现错误2

What is weird is that I tried to run the code with FireFox, I still have error 2

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

Is anything missing in my Post Ajax request? Is it possible to use Ajax to send data between 2 html files ?(I don't have any client/server side app here)

谢谢!

推荐答案

如果您需要将数据发送到此时要打开的新窗口,请改用GET并在url上传递参数.

If you need to send the data to a new window that you are going to open in that moment, use GET instead and pass the parameters on the url.

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

But, you could create an empty window first, then do your ajax and use the return content to fill your new window.

下面是一个示例,其中显示了打开窗口并用POST调用的结果填充窗口的例子: http://jsfiddle.net/bxhgr/

Here is an example fiddle opening the window and filling it with the result of a POST call: http://jsfiddle.net/bxhgr/

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>';
    }
});

这篇关于将数据从html页面传递到javascript中的另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆