简体   繁体   中英

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

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 made.

function getMeURL(){}

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

您不应该传递函数,但是可以传递函数的返回值。

url: GetMeUrl(args)

use ()

function getMeURL(){}

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

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

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