簡體   English   中英

如何修改Ajax函數的響應URL?

[英]How do I modify the response url of an ajax function?

我有一個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 ,這是我想要的行為。 但是,我想對success函數中的response進行一些小的操作-我想將協議從https更改為http 我怎么做?

我嘗試了response = response.replace('https', 'http'); 但這並沒有改變任何東西。

可能是我理解您的問題! 您希望成功將響應數據發送到執行此操作的同一頁面

首先,您必須創建一個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);
 }
 });
});

如果您覺得這沒有幫助,請對我進行調整。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM