jQuery的AJAX的形式

编程入门 行业动态 更新时间:2024-10-27 16:38:31
本文介绍了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的形式

本文发布于:2023-10-10 12:03:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形式   jQuery   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!