jQuery的AJAX的形式 - 如何获得重定向URL? [英] jquery ajax form - how to get the redirect url?

查看:187
本文介绍了jQuery的AJAX的形式 - 如何获得重定向URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ajax表单的jQuery插件,通过AJAX提交表单(在对话框中)。

这工作得很好,然后我得到的HTML响应从服务器返回。响应来自一个标准的重定向,后交PHP页面,我不能修改。

有没有办法获得这种重定向使用jQuery(Ajax回调中)的URL(最终GET位置)?

  $ J(下'span.sfAutocomplete一)。点击(函数(五){
    VAR URL = this.href;
    变量$对话框= $ J(下'< D​​IV ID =ajaxDialog>< / DIV>')。appendTo(身体)
    。加载(
      URL,
      sfAutocomplete =真正的',
      功能(responseText的,textStatus,XMLHtt prequest){
        $ dialog.dialog({的AutoOpen:真});
        //
        //阿贾克斯提交
        //
        附加$ J('#ajaxDialog形式)。递交(函数(){
          功能showResponse方法(responseText的,状态文本){

             //如何获得重定向URL?

          }
          附加$ J(本).ajaxSubmit({
            成功:showResponse方法
          });
          返回false;
        });
      }
    );
    返回false;
  });
 

解决方案

我没有使用过的插件,您正在使用,但如果你使用的 jQuery的阿贾克斯命令,您会收到XMLHtt prequest对象作为参数传递给完整事件。然后,你可以从返回的HTTP标头的帖子的网址。请尝试以下操作:

  $。阿贾克斯({
  网址:your.url,
  数据:你的数据,
  完成:功能(XHR,textstatus){
    // xhr.responseText包含来自服务器的响应
    变种allheaders = xhr.getAllResponseHeaders();
    //这将让所有的头一个字符串 - 如果你想让他们为对象...
    VAR eachheader = allheaders.split('\ N');
    变种头= {};
    对于(i = 0; I< eachheader.length;我++){
        如果($ .trim(eachheader [I])!==''){
            headersplit = eachheader [I] .split(:);
            。标题[headersplit [0]] = $修剪(headersplit [1]);
        }
    }
  }
});
 

这code是从的这个线程

i'm using ajax form jquery plugin to submit a form (in a dialog) via ajax.

this works fine and then i get the html response back from the server. the response comes from a standard redirect-after-post php page which i cannot modify.

is there a way to obtain the url of this redirect (the final GET location) using jquery (inside the ajax callback) ?

  $j('span.sfAutocomplete a').click(function(e){
    var url = this.href;
    var $dialog = $j('<div id="ajaxDialog"></div>').appendTo('body')
    .load(
      url,
      'sfAutocomplete=true',
      function (responseText, textStatus, XMLHttpRequest) {
        $dialog.dialog({ autoOpen: true });
        //
        //  Ajax submit
        //
        $j('#ajaxDialog form').submit(function() {
          function showResponse(responseText, statusText) {

             // how to get the redirect url ?

          }
          $j(this).ajaxSubmit({
            success: showResponse
          });
          return false; 
        }); 
      }
    );
    return false;
  });

解决方案

I haven't used the plug-in you are using, but if you use the jQuery Ajax command, you receive the XMLHttpRequest object as a parameter to the complete event. You can then get the post URL from the HTTP header that returns. Try the following:

$.ajax({
  url:'your.url',
  data:'your data',
  complete: function(xhr,textstatus) {
    // xhr.responseText contains the response from the server
    var allheaders = xhr.getAllResponseHeaders();
    // this will get all headers as a string - if you want them as an object...
    var eachheader = allheaders.split('\n');
    var headers = {};
    for(i = 0; i < eachheader.length; i++) {
        if ($.trim(eachheader[i]) !== '') {
            headersplit = eachheader[i].split(':');
            headers[headersplit[0]]=$.trim(headersplit[1]);
        }
    }
  }
});

This code was copied from this thread.

这篇关于jQuery的AJAX的形式 - 如何获得重定向URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆