简体   繁体   中英

define jQuery.ajax as a method

how can I define the jQuery ajax-method as a property of my object?

I have the ajax request

$.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:

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.

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"})
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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