pjax提交表单URL重定向

tim*_*son 5 javascript php codeigniter pjax pushstate

PJAX的文档指出Github上使用$.pjax.submit()在提交要点的形式.Github很好地实现的ajax表单提交的一个理想特性是URL从表单重定向action到新创建的URL(在这种情况下,一个包含新服务器端创建的gist ID).

例如,从这个:

https://gist.github.com/gists //  form action
Run Code Online (Sandbox Code Playgroud)

对此:

https://gist.github.com/tim-peterson/5019589 //assume this ID is generated server side
Run Code Online (Sandbox Code Playgroud)

我已经让这个在我的网站上工作类似(即,页面本身重定向到相当于https://gist.github.com/tim-peterson/5019589),但我不能重定向URL(它保持像https://gist.github.com/gists).

这完全是服务器端问题(设置标题?)还是pjax中有什么东西我缺少?我正在使用我今天下载的pjax版本,因此我不能使用pjax的buggier版本.

Ash*_*ora 5

你找到了解决方案吗?我有类似的问题.

基本上你需要在响应头中设置X-PJAX-URL属性.它的值应与请求URL相同.

在Laravel,我这样做了......

App::after(function($request, $response)
{
  $response->headers->set('X-PJAX-URL', $request->getRequestUri());
});
Run Code Online (Sandbox Code Playgroud)


Dav*_*ave 2

可能有一种更优雅/正确的方法来使用 $.pjax.submit() 来执行此操作,但这是我一直在使用的解决方案,没有任何问题。我在提交之前序列化表单输入,然后将它们发布到我的服务器。然后检查服务器的回复,确保在调用 PJAX 继续之前我不需要阻止用户继续。

$("#save").click(function(e) {

  e.preventDefault();
  dataString = $("#form").serialize();

  $.ajax({
    type: "POST",
    url: $("#form").attr("action"),
    data: dataString,
    dataType: "json",
    success: function(data)
    {
      // Handle the server response (display errors if necessary)

      $.pjax({
        timeout: 2000,
        url: data.url,
        container: '#main-content'
      });

    }
  });
});
Run Code Online (Sandbox Code Playgroud)