2016-03-02 113 views
0

我正在開發一個項目的頁面,該頁面試圖通過按鈕(有多個)id標籤分配特定值來創建ajax請求。這有效;該值被成功傳遞,每次點擊觸發ajax調用。AJAX中的動態變化變量獲取請求

當我嘗試使用不同的按鈕再次呼叫同一頁面時,將重新分配變量,但發送的GET請求保持不變

我如何通過一個新的變量(在這種情況下,ID)傳遞到GET請求?

function someAJAX(target) { 

    var trigger = [target.attr('id')]; 
    console.log[trigger]; 

    $.ajax({ 
     // The URL for the request 
     url: "onyxiaMenus/menuBase.php", 

     // The data to send (will be converted to a query string) 
     data: { 
      //class: target.attr("class"), 
      tableCall: true, 
      sort: trigger, 
      sortOrder: 'DESC', 
     }, 

     // Whether this is a POST or GET request 
     type: "GET", 

     // The type of data we expect back 
     //The available data types are text, html, xml, json, jsonp, and script. 
     dataType: "html", 

     // Code to run if the request succeeds; 
     // the response is passed to the function 
     success: function (data) { 
      console.log("AJAX success!"); 
      $('#prop').replaceWith(data); 

     } 
     , 

     // Code to run if the request fails; the raw request and 
     // status codes are passed to the function 
     error: function (xhr, status, errorThrown) { 
      console.log("Sorry, there was a problem!"); 
      console.log("Error: " + errorThrown); 
      console.log("Status: " + status); 
      console.dir(xhr); 
     } 
     , 

     // Code to run regardless of success or failure 
     complete: function (xhr, status) { 
      console.log("The request is complete!"); 
      $('#view').prepend(xhr); 

     } 


    }); 
} 

$(document).ready(function() { 

    $(".sort").on("click", function (e) { 
     //e.stopPropagation(); 
     //e.preventDefault(); 

     target = $(this); 
     //console.log(target.attr("class")); 
     console.log(target.attr("id")); 


     /* ADD CHILDREN TO ELEMENT*/ 
     if (target.hasClass('asc')) { 
      target.removeClass('asc') 
     } else { 
      target.addClass('asc') 
     } 

     /* MANAGE CLASS ADD/REMOVE FOR TARGET AND SIBLINGS */ 
     if (target.hasClass('btn-primary')) { 
     } else { 
      target.addClass('btn-primary') 
     } 

     someAJAX(target); 

     target.siblings().removeClass('btn-primary'); 


    }) 
}); 

回答

0

嘗試調用你的Ajax這樣someAJAX.bind(target)(); 然後在功能上成爲

function someAJAX() { 

$.ajax({ 
    // The URL for the request 
    url: "onyxiaMenus/menuBase.php", 

    // The data to send (will be converted to a query string) 
    data: { 
     //class: this.attr("class"), 
     tableCall: true, 
     sort: this.attr('id'), 
     sortOrder: 'DESC', 
    }, 

    // Whether this is a POST or GET request 
    type: "GET", 

    // The type of data we expect back 
    //The available data types are text, html, xml, json, jsonp, and script. 
    dataType: "html", 

    // Code to run if the request succeeds; 
    // the response is passed to the function 
    success: function (data) { 
     console.log("AJAX success!"); 
     $('#prop').replaceWith(data); 

    } 
    , 

    // Code to run if the request fails; the raw request and 
    // status codes are passed to the function 
    error: function (xhr, status, errorThrown) { 
     console.log("Sorry, there was a problem!"); 
     console.log("Error: " + errorThrown); 
     console.log("Status: " + status); 
     console.dir(xhr); 
    } 
    , 

    // Code to run regardless of success or failure 
    complete: function (xhr, status) { 
     console.log("The request is complete!"); 
     $('#view').prepend(xhr); 

    } 


}); 
} 
0

trigger似乎並沒有在任何地方定義。這是唯一可以在您的請求之間進行更改的數據,因爲其他數據是靜態編碼的。

你只需要確保trigger定義和兩個請求之間變化。

+0

觸發器在ajax函數的第二行中定義。實際的通話正在工作,但只有第一通電話。 –

+0

啊,我錯過了......究竟是什麼意思,它不是第二次工作。它在做什麼?你是否在控制檯中發現錯誤?同樣,目標是同一個元素的兩次函數被調用,如果它是隻是將相同的確切的調用..這將解釋爲什麼什麼都沒有發生.. –

0

感謝在這個問題上的投入。我深陷於我的問題的底部。我的請求被正確處理,但轉儲表正在創建語法錯誤,從而阻止將新信息附加到我的頁面。

感謝您的快速回復! 牆現在工作。