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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16871289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:18:04  来源:igfitidea点击:

How to redirect response from ajax request

javascriptjqueryajax

提问by hemanth

I need to redirect to a page from response. I made a ajax call and can handle success. There is html page in response, but how to redirect it to that page.
Here's my code.

我需要从响应重定向到页面。我做了一个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;                  
        }
    });
 });

采纳答案by Bergi

Not using ajax would make this easier:

不使用 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>

If you really want to use ajax, you should generate a distinct server response, containing only the HTML parts you want to update in your page or actual JSON.

如果你真的想使用 ajax,你应该生成一个不同的服务器响应,只包含你想要在页面或实际 JSON 中更新的 HTML 部分。

If you insist on using the response which you currently get, the appropriate way of dealing with it would be document.write:

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

回答by Ramaraj Karuppusamy

Please try this.

请试试这个。

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

回答by techfoobar

Your response is an object containing the full HTML for a page in the responseTextproperty.

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

You can probably do $(body).html(response.responseText);instead of window.location.href = ...;to overwrite the current page content with what you got a response.

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

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

But i suggest you don't and there could be style and other conflicts with whats already there on the page.

但是我建议您不要这样做,并且可能会与页面上已有的内容存在样式和其他冲突。

回答by tusharmath

In your HTML add a div with id as 'content', something like this

在您的 HTML 中添加一个 id 为“内容”的 div,类似这样

<div id='content'/>

Since your response is html in your complete function append the content into the div like this -

由于您的完整函数中的响应是 html,因此将内容附加到 div 中,如下所示 -

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

Let me know if you still face issues.

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

回答by Subhra Sekhar Mukhopadhyay

try this

试试这个

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