简体   繁体   中英

ajax API call Variable {variable} - How do I change it

How do I set a variable that is useable through a html page? It needs to be useable in javascript & ajax functions.

$.ajax({
    type: "POST",
    url: "/events/{account}/events",
    data: { name: "Marcus", location: "HERE" }
    }).done(function( msg ) {
    alert( "CREATE EVENT Data Saved: " + msg );
    });
    }

Everything around the variable {account} can change,but I need to use the same account number through the page. Thanks

You need to build up the URL dynamically, based on each account parameter. You could use a small reusable function for that:

function createEvent(account, eventData, callback) {
  var url = "/events/" + account + "/events";

  $.ajax({
    type: "POST",
    url: url,
    data: eventData
  }).done(callback);
}

Then, to use it:

var event = { name: "Marcus", location: "HERE" };

// Get some_account_number from somewhere...
createEvent(some_account_number, event, function(msg) {
  alert( "CREATE EVENT Data Saved: " + msg );
});

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