Javascript 如何重定向来自ajax请求的响应
我需要从响应重定向到一个页面。我打了一个ajax电话,可以处理成功。有html页面响应,但如何将其重定向到该页面。Javascript 如何重定向来自ajax请求的响应,javascript,jquery,ajax,Javascript,Jquery,Ajax,我需要从响应重定向到一个页面。我打了一个ajax电话,可以处理成功。有html页面响应,但如何将其重定向到该页面。 这是我的密码 $("#launchId").live('click',function(){ var id= $("#id").val(); var data = 'id='+id; $.ajax({ url: "xyz.json", type: "post", data: data, data
这是我的密码
$("#launchId").live('click',function(){
var id= $("#id").val();
var data = 'id='+id;
$.ajax({
url: "xyz.json",
type: "post",
data: data,
dataType: 'json',
complete : function(response) {
window.location.href = response;
}
});
});
试试这个
$("#launchId").live('click',function(){
var id= $("#id").val();
var data = 'id='+id;
$.ajax({
url: "xyz.json",
type: "post",
data: data,
dataType: 'json',
complete : function(response) {
window.location.href = '/yourlocation?'+response;
}
});
});
您的响应是一个对象,包含
responseText
属性中页面的完整HTML
您可能可以执行$(body.html(response.responseText)代码>而不是window.location.href=
用您得到的响应覆盖当前页面内容
...
complete : function(response) {
$(body).html(response.responseText);
}
但是我建议您不要这样做,并且可能会与页面上已有的内容存在样式和其他冲突。在HTML中添加一个id为“content”的div,类似这样
<div id='content'/>
如果您仍然面临问题,请告诉我。请尝试此功能
var newDoc = document.open("text/html", "replace");
newDoc.write(response.responseText);
newDoc.close();
不使用ajax会使这更容易:
<form type="POST" action="xyz.json">
<label for="id">Enter ID:</label><input id="id" name="id">
<button type="submit" id="launchId">Send</button>
</form>
live已被弃用@hemanth是一个url响应吗?添加一个console.log(JSON.stringify(response))
beforewindow.location.href=response代码>并在此处发布您得到的内容。这是否用于移动HTML网站?没有响应是一个包含全部HTML内容的对象请解释什么是“live method已弃用”@Baadshah@SubhraSekharMukhopadhyay请看这里kk-thanx的投票结果@Bergi@Subhra当前位置不是我,因此我无法删除否决票。答案仍然是无效的,因为op声称响应
是HTML标记,而不是URI部分。如果整个页面得到更新,最好他只是提交一个表单,对吗?@TusharMathur-是的,在这种情况下不需要AJAX帖子。
<form type="POST" action="xyz.json">
<label for="id">Enter ID:</label><input id="id" name="id">
<button type="submit" id="launchId">Send</button>
</form>
$.ajax({
url: "xyz.json",
type: "post",
data: data,
dataType: 'html', // it's no JSON response!
success: function(response) {
document.write(response); // overwrite current document
},
error: function(err) {
alert(err+" did happen, please retry");
}
});