javascript - 如何在javascript函数中接收ajax调用的POST数据

标签 javascript jquery twitter-bootstrap cordova datatables

我需要一点帮助。我正在尝试使用 ajax 分页实现 Bootstrap 数据表。由于这是移动应用程序(Cordova),我也完全依赖服务器端的 javascript。这就是为什么我在 url 中调用 javascript 函数。现在我不知道如何获取其中的 POST 数据。任何帮助将不胜感激。

$(document).ready(function(){
  $("‪#‎products‬").dataTable({
     aLengthMenu: [
       [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]
     ],
     aaSorting: [],
     bProcessing: true,
     serverSide: true,
    ajax:{
      url : ProductList.getProductsPaginated(),
      type: "post", // type of method , by default would be get
      error: function(){ // error handling code
    }
  }
});

ProductList.getProductsPaginated(){
// This method gets called succesfully
// Need the post data here
}
});

编辑: 让我们不使用函数作为 url 参数,直接使用 html 页面,如

url : "page.html",

现在,如何获取html或js中的post数据?对于我正在寻找的东西还有其他方法吗?

最佳答案

我不明白为什么你需要该函数内的数据,但它不会在ajax成功时被调用,它是因为括号而被调用的,我认为你唯一能做的就是返回你的 json 数据,但是你可以使用 ajax 成功时的发布数据调用一个函数,如下所示:

ajax: {
 url : ProductList.getProductsPaginated(),
 type: "post",
 dataSrc: function (data) {
  // you can call a function here with the data as parameter
  // you have to return the data, with modifications if you need
  return data;
 }
}

关于javascript - 如何在javascript函数中接收ajax调用的POST数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229274/

相关文章:

javascript - SettimeOut 间隔失败,出现 "Cannot convert undefined or null to object"

javascript - 如何复用函数属性?

Javascript getElementsByTagName ("div") 我在标记什么?

javascript - 根据 iframe 内的内容调整 iframe 及其父高度

html - 来自 Bootstrap 的奇怪行为,元素紧跟在图例之后

javascript - 图像 slider 不允许在移动 View 中下拉导航栏

javascript - ExtJS 5.0 - 添加按钮作为组件的覆盖

javascript - 获取 jQuery 中所有匹配项的 CSS 选择器

javascript - 防止div在上面加载数据后跳下Jquery

jquery - Bootstrap BS3 导航栏点击下拉子菜单