简体   繁体   English

处理Ajax重定向并将内容加载到新页面

[英]Handle Ajax redirect and load content into new page

I am using Ajax to dynamically update a data table without refreshing. 我正在使用Ajax来动态更新数据表而不刷新。 I have no problem so far. 到目前为止,我没有问题。 The user selects certain criteria from a Form . 用户从“表单”中选择某些条件。 The issue is that , I no longer want to fetch content to this same page but I want to redirect and load content on a different template: 问题是,我不再希望将内容提取到同一页面,而是希望将内容重定向并加载到其他模板上:

Users selection ( Page1.html ) -> Ajax -> redirect to Page2.html -> load data within Page2 用户选择( Page1.html )-> Ajax- >重定向到Page2.html- >在Page2中加载数据

Would someone please have a look at the code below and advise why it is not working ? 有人可以看看下面的代码,并建议为什么它不起作用吗? I am able to redirect but there isn't any returned data. 我可以重定向,但是没有任何返回的数据。

Ajax 阿贾克斯

if (t && e ) {
    dataArray = new Array;
    dataArray[0] = e;
    dataArray[1] = t;

 $.ajax({
   type: "POST",
   url: "includes/filter.php",
   data: {
       Name: e,
       Color: t,
          },

        success: function (e) {           

        window.location.href = 'Page2.html';  // Redirect to this page
        $("#Wrapper").html(e);    // Load content to this page in Div # Wrapper

        // If I Uncomment the two lines above and just add $("#table").html(e); it will successfully load content within the table div on the same page

        }
    });
  }

Where filter.php handles the server side query and outputs an html table with the data. 其中filter.php处理服务器端查询并输出带有数据的html表。 THanks 谢谢

Pass e and t to your next page like 将e和t传递到下一页,例如

window.location.href = 'Page2.html?e=' + e + '&t=' + t;

Once page2 is loaded, get e and t and make your ajax call and replace html of wrapper. 加载page2后,获取e和t并进行ajax调用,并替换包装器的html。 In your current solution page2 is loaded before wrapper html gets replaced. 在您当前的解决方案中,在替换包装HTML之前先加载page2。

for creating query string from array: 用于从数组创建查询字符串:

var array = [],
array["Name"] = "Car";
array["Color"] = "Red";
var queryStr = "";

for (var key in array){
  if(queryStr != "") queryStr += "&";
  queryStr += key + "=" + array[key];
}

var url = "Page2.html?" + queryStr;

Try this: 尝试这个:

if (t && e ) {
        dataArray = new Array;
        dataArray[0] = e;
        dataArray[1] = t;

     $.ajax({
       type: "POST",
       url: "includes/filter.php",
       data: {
           Name: e,
           Color: t,
              },

            success: function (data) {           

            window.location.href = 'Page2.html?data='+data ;  // Redirect to this page


            }
        });
      }

After send to the request to the page using POST. 使用POST发送到页面请求之后。 And add this line in page2.html $("#Wrapper").html(e); 并将此行添加到page2.html $(“#Wrapper”)。html(e);中。

I would suggest to clearly choose your path : 我建议您明确选择路径:

  • Option 1 : drop the ajax request, and do open a new window 选项1:删除ajax请求,并打开一个新窗口

    window.open('Page2.html?filter=yabayabayaba')

  • Option 2 : use an ajax request, and load the result in a dialog box (using bootstrap or jquery-ui) 选项2:使用ajax请求,并将结果加载到对话框中(使用bootstrap或jquery-ui)

    success: function (e) { $(e).dialog(); }

There are a few things wrong here. 这里有些错误。

  1. When window.location.href = 'Page2.html'; window.location.href = 'Page2.html'; executes it stops the rest of the code from executing and redirects the browser. 执行它会停止执行其余代码,并重定向浏览器。

  2. The $("#Wrapper").html(e); $("#Wrapper").html(e); is in your AJAX success function. 在您的AJAX成功功能中。 So it will only ever fire after the AJAX call. 因此,它将仅在AJAX调用后触发。 When the new page loads this code isn't executed. 加载新页面时,不会执行此代码。 (It will never fire anyway because of the redirect in front of it). (因为它前面的重定向,它永远不会触发)。

The whole point of AJAX is to not redirect the user. AJAX的全部目的是不重定向用户。 If you're redirecting them anyway then why not just have the correct content in Page2.html when it loads rather than inserting it with javascript? 如果仍然要重定向它们,那么为什么在加载时不仅仅在Page2.html中包含正确的内容,而不是使用javascript插入内容?

Alternatively, you could drop the redirect and just load the entire Page2.html file into your current page in place of the original template using an AJAX request. 另外,您可以删除重定向,然后使用AJAX请求将整个Page2.html文件加载到当前页面中,以代替原始模板。

I don't know what your HTML looks like but if Page2.html has a div named #wrapper all you need to do is load everything from inside #wrapper of Page2.html into the #wrapper of Page1.html like this: 我不知道您的HTML是什么样,但是如果Page2.html具有一个名为#wrapper的div,则您需要做的就是将Page2.html的#wrapper内部的所有内容加载到Page1.html的#wrapper中,如下所示:

$('#wrapper').load('Page2.html #wrapper');

That code you should go in your AJAX success function in place of the redirect. 该代码应该放在AJAX成功函数中,以代替重定向。 You can see more about .load() here . 您可以在此处查看有关.load()的更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM