ajax API调用变量{variable} - 如何更改它

时间:2013-08-05 17:33:13

标签: javascript jquery ajax

如何设置可通过html页面使用的变量?它需要在javascript和amp;中使用ajax功能。

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

变量{account}周围的所有内容都可以更改,但我需要在页面中使用相同的帐号。感谢

1 个答案:

答案 0 :(得分:1)

您需要根据每个account参数动态构建URL。你可以使用一个小的可重用函数:

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

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

然后,使用它:

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