开发者

Is it possible to pass a function to the url field in an ajax call?

开发者 https://www.devze.com 2023-04-12 20:56 出处:网络
Is it possible to pass a function to the url field in an ajax call? I want to dynamically pass a different url to the ajax call mad开发者_Python百科e.

Is it possible to pass a function to the url field in an ajax call? I want to dynamically pass a different url to the ajax call mad开发者_Python百科e.

function getMeURL(){}

$.ajax({
  url: getMeURL,
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});


You shouldn't pass a function, but you can certainly pass the return value of a function.

url: GetMeUrl(args)


You can pass the result of a function, but not a function itself as you seem to be trying to do. Here's an updated example (notice the parentheses):

function getMeURL() {
    return "http://www.example.com";
}
$.ajax({
  url: getMeURL(),
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});


use ()

function getMeURL(){}

$.ajax({
  url: getMeURL(),
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消