使用 POST 将变量传递到使用 Jquery 的另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4350674/
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-26 17:07:08  来源:igfitidea点击:

Passing variables with POST to another page with Jquery

jquerypostredirect

提问by sarsnake

I am relatively new to Jquery and I was wondering how would one post variables to another page and then redirect? I used ajax function, the redirect works fine, but no variables are captured in POST (they are empty)

我对 Jquery 比较陌生,我想知道如何将变量发布到另一个页面然后重定向?我使用了 ajax 函数,重定向工作正常,但在 POST 中没有捕获任何变量(它们是空的)

function linkWO() {

    $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "LinkTagOut.aspx",
            dataType: "json",
            data: "{id=1}",
            complete:
            function () {
                window.location = "LinkTagOut.aspx";
            }

    });
}

in my aspx file

在我的 aspx 文件中

<a href="javascript:void(0);" onclick="return linkWO();"><span>Link</span></a>

采纳答案by kobe

This answer is just for a quick fix

这个答案只是为了快速修复

why don't you just pass as query string here

你为什么不在这里作为查询字符串传递

window.location = "LinkTagOut.aspx?variabletopass=test"; 
<form id="target" action="destination.html">
  <input type="text" value="Hello there" />
  <input type="submit" value="Go" />
</form>
<div id="other">
  Trigger the handler
</div>

$('#target').submit(function() {
$.post('ajax/test.html', function(data) {      
});
  return false;
});

回答by karim79

$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "LinkTagOut.aspx",
        dataType: "json",
        data: { id: 1 }, // or the string: 'id=1'
        complete:
        function () {
            window.location = "LinkTagOut.aspx";
        }

});

From the $.ajax documentation(dataoption):

$.ajax 文档data选项):

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

要发送到服务器的数据。如果不是字符串,则将其转换为查询字符串。它附加到 GET 请求的 url。请参阅 processData 选项以防止此自动处理。对象必须是键/值对。如果 value 是一个数组,jQuery 会根据传统设置的值(如下所述)使用相同的键序列化多个值。

Also, make sure to return falsefrom the end of the submit handler (or whatever fires the ajax call) to ensure that a 'normal' redirect is not happening.

此外,请确保return false从提交处理程序的末尾(或任何触发 ajax 调用的内容)以确保不会发生“正常”重定向。

回答by Alejo Toro

If you like to code less do this:

如果你喜欢少写代码,请执行以下操作:

  1. include jquery.redirect.min.jsin your javascript (js) folder.
  2. Load file AFTER JQUERY loading script:

    <script type='text/javascript' src='jquery.js'> 
    </script>
    
    <script type='text/javascript' src='jquery.redirect.min.js'>
    </script>
    
  3. Simply replace desired parameters on the next line (copy/paste) in your javascript code:

    $().redirect('targeturl.html', {'post_var_1': 'value1', 'post_var_2': 'value2'});
    
  1. jquery.redirect.min.js包含在您的 javascript (js) 文件夹中。
  2. 在 JQUERY 加载脚本之后加载文件:

    <script type='text/javascript' src='jquery.js'> 
    </script>
    
    <script type='text/javascript' src='jquery.redirect.min.js'>
    </script>
    
  3. 只需在您的 javascript 代码中替换下一行(复制/粘贴)所需的参数:

    $().redirect('targeturl.html', {'post_var_1': 'value1', 'post_var_2': 'value2'});
    

This is the simplest and the fastest method I have found for posting variables to another page without using the form tag. Good luck!

这是我找到的最简单和最快的方法,可以在不使用表单标签的情况下将变量发布到另一个页面。祝你好运!

回答by Ender

Have a look at the "data" option on this doc page: http://api.jquery.com/jQuery.ajax/

看看这个文档页面上的“数据”选项:http: //api.jquery.com/jQuery.ajax/

Your problem is that you are trying to pass a json string (and it is valid to pass a string), but if you pass a string, jQuery is expecting a parameterized query string. If you want to pass a json object, it should not be a string.

您的问题是您正在尝试传递一个 json 字符串(并且传递一个字符串是有效的),但是如果您传递一个字符串,jQuery 需要一个参数化的查询字符串。如果你想传递一个json对象,它不应该是一个字符串。

However, note that json objects passed this way will be converted to a parameterized query string by jQuery, so if it's not inconvenient (as in this case, where you only have one value), you might as well just pass it that way to begin with and save the script some work.

但是,请注意,以这种方式传递的 json 对象将被 jQuery 转换为参数化查询字符串,因此如果它不方便(例如在这种情况下,您只有一个值),您不妨以这种方式传递它开始使用并保存脚本一些工作。