如何重定向来自ajax请求的响应

我需要从响应重定向到页面。 我做了一个ajax调用,可以处理成功。 有html页面作为响应,但如何将其重定向到该页面。
这是我的代码。

$("#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; } }); }); 

不使用ajax会使这更容易:

 

如果您确实想使用ajax,则应生成一个独特的服务器响应,仅包含要在页面中更新的HTML部分或实际的JSON。

如果你坚持使用你当前得到的响应,处理它的适当方法是document.write

 $.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"); } }); 

请试试这个。

 var newDoc = document.open("text/html", "replace"); newDoc.write(response.responseText); newDoc.close(); 

您的响应是一个对象,其中包含responseText属性中页面的完整HTML。

你可以做$(body).html(response.responseText); 而不是window.location.href = ...; 用你得到的回复覆盖当前页面内容。

 ... complete : function(response) { $(body).html(response.responseText); } 

但我建议你不要,并且可能存在风格和其他与页面上已有的冲突。

在你的HTML中添加一个id为’content’的div,就像这样

 

由于您的响应是完整函数中的html,因此将内容附加到div中 –

  complete : function(response) { $('#content').append(response.responseText); } 

如果您仍然遇到问题,请告诉我。

试试这个

 $("#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; } }); });