简体   繁体   English

将jQuery.ajax定义为方法

[英]define jQuery.ajax as a method

how can I define the jQuery ajax-method as a property of my object? 如何定义jQuery ajax方法作为对象的属性?

I have the ajax request 我有ajax请求

$.ajax({
    type: "POST",
    url: "../PHP/RoadtripsTable.php",
    data: ({fnChoice: "listRoadtrips"})
})

and I would like to define a property x of an object o like this: 我想像这样定义对象o的属性x:

var o = {
    x: $.ajax({
        type: "POST",
        url: "../PHP/RoadtripsTable.php",
        data: ({fnChoice: "listRoadtrips"})
    })
}

but this does not seem to work. 但这似乎不起作用。 I need ox to be a deferred since I am using it inside of $.() from the jQuery-Library. 我需要推迟使用ox,因为我是在jQuery-Library的$。()中使用它的。

This should work for you: 这应该为您工作:

var o = {
  x: function() {
    $.ajax({
      type: "POST",
      url: "../PHP/RoadtripsTable.php",
      data: ({fnChoice: "listRoadtrips"})
    });
  }
}

This alternative declaration should also work: 此替代声明也应起作用:

var o = {
  x() {
    $.ajax({
      type: "POST",
      url: "../PHP/RoadtripsTable.php",
      data: ({fnChoice: "listRoadtrips"})
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM