2016-03-02 108 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

感谢在这个问题上的投入。我深陷于我的问题的底部。我的请求被正确处理,但转储表正在创建语法错误,从而阻止将新信息附加到我的页面。

感谢您的快速回复! 墙现在工作。