使用jQuery传递POST数据时打开URL

时间:2010-03-02 23:09:15

标签: jquery

使用jQuery在将帖子数据传递到新页面时是否可以更改页面URL?

5 个答案:

答案 0 :(得分:12)

如果您想要更改当前页面网址,那么您可以向当前页面添加新的<form>,向其中添加隐藏的输入元素,然后提交它

$('body').append($('<form/>')
  .attr({'action': yourNewURL, 'method': 'post', 'id': 'replacer'})
  .append($('<input/>')
    .attr({'type': 'hidden', 'name': 'param1', 'value': "hello"})
  )
  .append($('<input/>')
    .attr({'type': 'hidden', 'name': 'param2', 'value': "world"})
  )
).find('#replacer').submit();

或类似的东西

答案 1 :(得分:4)

尝试以下功能。适合我。 JSON兼容。

/**
 * Function that will redirect to a new page & pass data using submit
 * @param {type} path -> new url
 * @param {type} params -> JSON data to be posted
 * @param {type} method -> GET or POST
 * @returns {undefined} -> NA
 */
function gotoUrl(path, params, method) {
    //Null check
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    //Fill the hidden form
    if (typeof params === 'string') {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", 'data');
        hiddenField.setAttribute("value", params);
        form.appendChild(hiddenField);
    }
    else {
        for (var key in params) {
            if (params.hasOwnProperty(key)) {
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", key);
                if(typeof params[key] === 'object'){
                    hiddenField.setAttribute("value", JSON.stringify(params[key]));
                }
                else{
                    hiddenField.setAttribute("value", params[key]);
                }
                form.appendChild(hiddenField);
            }
        }
    }

    document.body.appendChild(form);
    form.submit();
}

演示用法

    <script>
        $('#GotoNextPage').on('submit', function(evt){
            evt.preventDefault();

            //Make the data
            var sampleData = new Object();
            sampleData.currentPage = 'We are here';
            sampleData.currentUid = 5;

            gotoUrl("actionPage.php", {'cmd':'nextPage', 'data':sampleData});
        });
    </script>

希望这有帮助!

答案 2 :(得分:2)

如果您在表单中发布数据,因为您只需将表单的操作设置为当前页面),并且发布的数据不直接相关对于下一页的内容,您可以使用jQuery's .post() method的回调函数来重定向用户:

$.post( '/Page2', { 'foo' : 'bar' }, function() {
    window.location.href = '/Page2';
});

这似乎是一种不太可能的情况,也许您可​​以评论有关传递的数据及其与Page2内容的关系的更多细节?

答案 3 :(得分:1)

使用jquery post method

将变量保存到$_SESSION

jquery代码

$.post("save_session.php", { out: output }) .done(function(data) {  
location.href='your_link.php';
});

Php代码(save_session.php)

$_SESSION["variables"]=$_POST["out"];

Php代码(your_link.php)

$output=$_SESSION["variables"];

答案 4 :(得分:0)

诀窍是使用php会话将数据保留在目标页面中

AJAX POST

 $(button).click(function(){
    var s_name=$("#txtbox").val(); 
 $.post("http://localhost/AJAX/save_session.php",
{
  student_name: s_name,

},
function(data){

  location.href='http://localhost/AJAX/result.php';
});

保存会话PHP

<?php
session_start();
$_SESSION["name"]=$_POST['student_name'];

?>

最后是您的目标页面

targetpage.php

    <?php
  session_start();
  include("config.php");
  $name=$_SESSION["name"];
?>

现在,您可以使用$ name或定义的变量来传递数据