gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:53:46 24 4
gpt4 key购买 nike

我需要从响应重定向到一个页面。我进行了 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 会使这更容易:

<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,你应该生成一个独特的服务器响应,只包含你想在你的页面或实际 JSON 中更新的 HTML 部分。

如果您坚持使用您当前获得的响应,则适当的处理方式是 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");
}
});

关于javascript - 如何重定向来自ajax请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16871289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com