如何修改Ajax函数的响应URL?

时间:2018-11-07 10:54:35

标签: javascript jquery ajax

我有一个ajax函数,成功后会重定向到URL返回的响应:

document.getElementById("button1").addEventListener("click", function (e) {
    e.preventDefault();
    $.ajax({
        url: 'http://localhost:8080/someLoginUrl',
        method: 'GET',
        crossDomain : true,
        xhrFields: {
            withCredentials: false
        },
        success: function(response){
            window.location.href = response;
        }
    });
});

http://localhost:8080/someLoginUrl返回的响应只是一个包含URL的网页:

这是响应页面的html:

<html><head></head><body>https://localhost:8080/anotherLogin</body></html>

因此,一旦我单击按钮button1,而不是重定向到http://localhost:8080/someLoginUrl,它就会重定向到https://localhost:8080/anotherLogin,这是我想要的行为。 但是,我想对response函数内的success进行一些小的操作-我想将协议从https更改为http。我该怎么办?

我尝试了response = response.replace('https', 'http');,但这并没有改变任何东西。

1 个答案:

答案 0 :(得分:0)

我可以理解您的问题吗?您希望成功将响应数据发送到执行此操作的同一页面上

首先,您必须创建一个ID为 <div id=res_data> </div> 的潜水,并将该div放置在要显示响应数据的位置。

那么您的最终代码是

document.getElementById("button1").addEventListener("click", function (e) {
  e.preventDefault();
  $.ajax({
  url: 'http://localhost:8080/someLoginUrl',
  method: 'GET',
  crossDomain : true,
  xhrFields: {
  withCredentials: false
  },
 success: function(response){
    $('#res_data').val(response);
 }
 });
});

如果没有帮助,请调整我。