簡體   English   中英

在模態視圖后在外部js文件中進行調用時,jQuery ajax調用正在復制URL

[英]jquery ajax call is duplicating the url, when making a call in external js-file after modal-view

很抱歉,我的問題很難解決。 嘗試進行AJAX調用時,我面臨一個奇怪的行為。 在我的網站上,我有一個按鈕,該按鈕允許用戶使用REST API調用從我的數據庫中刪除一些數據。 當我直接在html文件中的<script>標記內進行調用時,一切都會按預期進行。 如預期的那樣,對地址http://0.0.0.0:8080/v1/item/id1進行了刪除呼叫

<script>
  $("#removeButton").click(function(e){
    bootbox.confirm("Are you sure?", function(result) {
    if (result == true){
      $.ajax({
        type: "DELETE",
        async: true,
        contentType: "application/json",
        url: "/v1/item/" + itemID,    
            dataType: "json",    
            success:function(result){               
            location.reload()
        },
        error:function(){
          $("#removeButton").notify("Error in removing item", "error");
        }
        }); 
    }
    else{
      console.log("false");
    }
  });
</script>

但是,我試圖擺脫html文件中的大部分javascript代碼,並將大部分代碼放入外部.js文件中。 當我將該AJAX調用放入外部js文件中的函數時,AJAX url就會以某種方式重復。 它嘗試撥打地址0.0.0.0:8080/v1/item/http://0.0.0.0:8080/www/item/id1 正確的網址應為0.0.0.0:8080/v1/item/id1 HTML文件位於0.0.0.0:8080/www/item/id1 我相信我正在使用的這個模式視圖( bootbox.js )也是問題的根源。

這是外部js文件button.js的內容:

function removeConfirmButtonPressed(){
  $.ajax({
    type: "DELETE",
    async: true,
    contentType: "application/json",
    url: "/v1/items/" + itemID,   
        dataType: "json",    
        success:function(result){               
        location.reload()
    },
    error:function(){
      $("#removeButton").notify("Error in removing item", "error");
    }
  }); 
}

這是html文件:

<script>
  $(document).ready(function(){
    //event for clicking remove button
    $("#removeButton").click(function(e){
      bootbox.confirm("Are you sure?", function(result) {
      if (result == true){
        removeConfirmButtonPressed()
      }
      else{
        console.log("false");
      }
      });
    });
  });       
</script>

所以我的問題是,當用戶確認他的選擇是Bootstrap模態視圖后,從外部js文件進行AJAX調用時,URL的這種奇怪重復是什么導致的?

看來itemID == "http://0.0.0.0:8080/www/item/id1" 您忘記僅獲得itemID對象的文本內容。

暫無
暫無

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

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