Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

ajax - passing data from an html page to another in 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

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

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

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

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

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

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)

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...