将ajax调用的respinse发布到另一个页面

时间:2017-07-05 21:41:11

标签: jquery ajax

我从一个ajax调用得到一个Json数组作为响应,我想立即将这个Json数组数据发布到另一个页面并重定向到该页面。有谁知道我该怎么做

 $.post("flightsearch/searchhome.php",{
                    fromairport2  : fromairport1,
                    toairport2    : toairport1
                }, function(data){
                       alert(data);
                       //var response = data;
                       //window.location.replace("flight-detail.php");
                });

我想发布我到达该页面的响应数据并同时重定向到该页面

1 个答案:

答案 0 :(得分:0)

一旦您捕获了JSON,就可以使用相关的输入字段创建表单的jQuery对象。创建表单后,提交它。我只是删除它以防止它在加载过程中显示在用户屏幕上。



/* Bind Click Event to Button */
$('button').on('click', function() {
    /* Set Empty jQuery Form Object */
    var formObject = $('<form/>', { method: 'POST' });
    
    /* Fetch JSON */
    $.get('https://jsonplaceholder.typicode.com/posts/1', function(response) {
        /* Itererate Through Response */
        $.each(response, function(key, value) {
            /* Append jQuery Input Object w/ Associated Name/Value to Form Object */
            $('<input/>', { name: key, value: value }).appendTo(formObject);
        });
        
        /* Append Form Object to Body, Submit, and Remove */
        formObject.appendTo('body').submit().remove();
    });
});

/* Delegating Form Submission on Body to Prevent Submission to Show Console Log */
$('body').on('submit', 'form', function(event) {
  event.preventDefault();
  console.log($(this).serializeArray());
});
&#13;
<body>
    <button>Fetch JSON and POST</button>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
&#13;
&#13;
&#13;