Javascript Ajax - 在“成功”后转到另一个页面,从那里的 url 检索用户 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28237224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:26:02  来源:igfitidea点击:

Ajax - after "success" go to another page retrieve user id from the url there

javascriptajaxjsonrest

提问by Zyg

var JSONonj = {"name": "James","age":"25"};
var data = "This data i would like to retrieve and print in the main.html page after success call in ajax.";

$.ajax({
        url: "/user",
        type: "POST",
        data: JSONobj,
        dataType: "json",
        success: function() {
            window.location.href = "main.html";
        },
        contentType: "application/json"
});

Here is the deal. After success i want to redirect to main.html and when I get there, I would like to retrieve and print data variable there.

这是交易。成功后我想重定向到 main.html,当我到达那里时,我想在那里检索和打印数据变量。

The redirection works fine. But I can not receive the data there.

重定向工作正常。但我无法在那里接收数据。

回答by LeGEC

There are two main ways to achieve that :

有两种主要方法可以实现:

  1. somehow send the data to the server (see the other answers for possible ways to do that)
  2. use the browser's localStorage:

    success: function() {
        localStorage.setItem('myMainKey', data);
        window.location.href = "main.html";
    }
    
    // in main.html's javascript :
    var data = localStorage.getItem('myMainKey');
    if (data !== undefined) {
        //do something
    }
    
  1. 以某种方式将数据发送到服务器(有关可能的方法,请参阅其他答案)
  2. 使用浏览器的localStorage

    success: function() {
        localStorage.setItem('myMainKey', data);
        window.location.href = "main.html";
    }
    
    // in main.html's javascript :
    var data = localStorage.getItem('myMainKey');
    if (data !== undefined) {
        //do something
    }
    

Note : if you want to store and retrieve a complete javascript object, you would have to use JSON.stringify()/ JSON.parse()for storing / retrieving it.

注意:如果要存储和检索完整的 javascript 对象,则必须使用JSON.stringify()/JSON.parse()来存储/检索它。

回答by T McKeown

You would have to send that data in the querystring and reconstruct it with javascript. Or you would have to POST to a handler/controller (who knows what you are using..) with that json data and reconstruct it... either way you if you are going to change the location you will have to send the data.

您必须在查询字符串中发送该数据并使用 javascript 重建它。或者,您必须使用该 json 数据向处理程序/控制器(谁知道您在使用什么……)发送 POST 并重建它……无论哪种方式,如果您要更改位置,您都必须发送数据。

If you are using HTML5 then local storage would be a good option as LeGEC advised.

如果您使用的是 HTML5,那么正如 LeGEC 所建议的那样,本地存储将是一个不错的选择。

Storing Objects in HTML5 localStorage

在 HTML5 localStorage 中存储对象

回答by Neto

Here's function that can be applied if you're using jQuery.

如果您使用 jQuery,则可以应用以下函数。

var redirect = 'http://www.example.com/';
$.redirectPost(redirect, {x: 'exemplo', y: '123'});

// jquery extend function
$.extend(
{
    redirectPost: function(location, args)
    {
        var form = '';
        $.each( args, function( key, value ) {
            value = value.split('"').join('\"')
            form += '<input type="hidden" name="'+key+'" value="'+value+'">';
        });
        $('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
    }
});

回答by Sheshant Sinha

you can also do this:-

你也可以这样做:-

window.location.href="main.html?data="+data;

In main.html

在 main.html

  var user = decodeURI(getUrlVars()["src"]);
   `//Now do some stuff with this data`

   `function getUrlVars() {
    var vars = [], hash;
    var hashes = 
    window.location.href.slice(window.location.href.indexOf('?') + 
    1).split('&');
   for (var i = 0; i < hashes.length; i++) {
   hash = hashes[i].split('=');
   vars.push(hash[0]);
   vars[hash[0]] = hash[1];
   }
   return vars;
   }`