简体   繁体   English

如何使用JQuery将数据从一个HTML页面传递到另一HTML页面?

[英]How to pass data from one HTML page to another HTML page using JQuery?

I have two HTML pages that work in a parent-child relationship in this way: 我有两个以这种方式在父子关系中工作的HTML页面:

The first one has a button which does two things: First it requests data from the database via an AJAX call. 第一个具有一个按钮,该按钮执行以下两项操作:首先,它通过AJAX调用从数据库请求数据。 Second it directs the user to the next page with the requested data, which will be handled by JavaScript to populate the second page. 其次,它将用户引导到具有所请求数据的下一页,JavaScript将对其进行处理以填充第二页。

I can already obtain the data via an ajax call and put it in a JSON array: 我已经可以通过ajax调用获取数据并将其放入JSON数组中:

$.ajax({
    type: "POST",
    url: get_data_from_database_url,
    async:false,
    data: params,
    success: function(json)
    {
        json_send_my_data(json);
    }
});

function json_send_my_data(json)
{
    //pass the json object to the other page and load it
}

I assume that on the second page, a "document ready" JavaScript function can easily handle the capture of the passed JSON object with all the data. 我假设在第二页上,一个“文档就绪”的JavaScript函数可以轻松地处理所有数据的传递JSON对象的捕获。 The best way to test that it works is for me to use alert("My data: " + json.my_data.first_name); 最好的测试方法是使用alert("My data: " + json.my_data.first_name); within the document ready function to see if the JSON object has been properly passed. 在文档准备功能中查看是否已正确传递JSON对象。

I simply don't know a trusted true way to do this. 我只是不知道执行此操作的可靠方法。 I have read the forums and I know the basics of using window.location.url to load the second page, but passing the data is another story altogether. 我已经阅读了论坛,并且知道使用window.location.url加载第二页的基本知识,但是传递数据完全是另一回事了。

session cookie may solve your problem. 会话cookie可能会解决您的问题。

On the second page you can print directly within the cookies with Server-Script tag or site document.cookie 在第二页上,您可以直接在带有Server-Script标签或站点document.cookie的cookie中进行打印。

And in the following section converting Cookies in Json again 在以下部分中,再次在Json中转换Cookies

How about? 怎么样?

You have two options I think. 我认为您有两种选择。

1) Use cookies - But they have size limitations. 1)使用Cookie-但它们有大小限制。

2) Use HTML5 web storage . 2)使用HTML5网络存储

The next most secure, reliable and feasible way is to use server side code. 第二种最安全,可靠和可行的方法是使用服务器端代码。

Disclaimer: This is terrible, but here goes: 免责声明:这很糟糕,但是这里有:

First, you will need this function (I coded this a while back). 首先,您将需要此功能(我之前对此进行了编码)。 Details here: http://refactor.blog.com/2012/07/13/porting-javas-getparametermap-functionality-to-pure-javascript/ 此处的详细信息: http : //refactor.blog.com/2012/07/13/porting-javas-getparametermap-functionality-to-pure-javascript/

It converts request parameters to a json representation. 它将请求参数转换为json表示形式。

function getParameterMap () {
    if (window.location.href.indexOf('?') === (-1)) {
        return {};
    }
    var qparts = window.location.href.split('?')[1].split('&'),
        qmap   = {};
    qparts.map(function (part) {
        var kvPair = part.split('='),
            key    = decodeURIComponent(kvPair[0]),
            value  = kvPair[1];

        //handle params that lack a value: e.g. &delayed=
        qmap[key] = (!value) ? '' : decodeURIComponent(value);
    });
    return qmap;
}

Next, inside your success handler function: 接下来,在您的成功处理函数中:

success: function(json) {
    //please really convert the server response to a json
    //I don't see you instructing jQuery to do that yet!
    //handleAs: 'json'

    var qstring = '?';
    for(key in json) {
        qstring += '&' + key + '=' + json[key];
        qstring = qstring.substr(1); //removing the first redundant &
    }
    var urlTarget = 'abc.html';
    var urlTargetWithParams = urlTarget + qstring;

    //will go to abc.html?key1=value1&key2=value2&key2=value2...
    window.location.href = urlTargetWithParams;
}

On the next page, call getParameterMap. 在下一页上,调用getParameterMap。

var jsonRebuilt = getParameterMap();
//use jsonRebuilt 

Hope this helps (some extra statements are there to make things very obvious). 希望这会有所帮助(有一些额外的说明可以使事情变得很明显)。 (And remember, this is most likely a wrong way of doing it, as people have pointed out). (记住,正如人们指出的那样,这很可能是错误的方法)。

Here is my post about communicating between two html pages, it is pure javascript and it uses cookies: Javascript communication between browser tabs/windows 这是我有关两个html页面之间进行通信的帖子,它是纯JavaScript并使用cookie: 浏览器选项卡/窗口之间的JavaScript通信

you could reuse the code there to send messages from one page to another. 您可以在那里重复使用代码以将消息从一页发送到另一页。

The code uses polling to get the data, you could set the polling time for your needs. 该代码使用轮询来获取数据,您可以根据需要设置轮询时间。

Warning: This will only work for single-page-templates, where each pseudo-page has it's own HTML document. 警告:这仅适用于单页面模板,其中每个伪页面都有其自己的HTML文档。

You can pass data between pages by using the $.mobile.changePage() function manually instead of letting jQuery Mobile call it for your links: 您可以通过手动使用$.mobile.changePage()函数在页面之间传递数据,而不是让jQuery Mobile为您的链接调用它:

$(document).delegate('.ui-page', 'pageinit', function () {
    $(this).find('a').bind('click', function () {
        $.mobile.changePage(this.href, {
            reloadPage : true,
            type       : 'post',
            data       : { myKey : 'myVal' }
        });
        return false;
    });
});

Here is the documentation for this: http://jquerymobile.com/demos/1.1.1/docs/api/methods.html 这是有关此文档: http : //jquerymobile.com/demos/1.1.1/docs/api/methods.html

You can simply store your data in a variable for the next page as well. 您也可以简单地将数据存储在下一页的变量中。 This is possible because jQuery Mobile pages exist in the same DOM since they are brought into the DOM via AJAX. 这是可能的,因为jQuery Mobile页面存在于同一DOM中,因为它们是通过AJAX引入DOM的。 Here is an answer I posted about this not too long ago: jQuery Moblie: passing parameters and dynamically load the content of a page 这是我不久前发布的答案: jQuery Moblie:传递参数并动态加载页面内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何将数据从一个页面传递到另一个页面html - How to pass data from one page to another page html 如何在html中将数据从一个页面传递到另一个页面? - How to pass data from one page to another page in html? 在angularJS中将数据从一个html页面传递到另一个html页面 - Pass data from one html page to another html page in angularJS 如何将javascript / jquery函数从一个HTML页面传递到另一HTML页面 - How to pass a javascript/jquery function from one html page to another 如何使用Phonegap中的JavaScript将数据从一个html传递到另一个html页面? - How to pass the data from one html to another html page using JavaScript in Phonegap? 如何将数据从一个html页面传递到另一个? - How to pass data from one html page to another? 如何将数据从html页面传递到另一个页面? - How to pass data from a html page to another? 如何使用javascript将值从一个html页面传递到另一个html页面 - How can I pass values from one html page to another html page using javascript 如何使用 JavaScript 将数据从一个 HTML 页面传递到另一个页面 - How can i pass data from one HTML page to another using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM