简体   繁体   English

是否可以在ajax调用中将函数传递给url字段?

[英]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? 是否可以在ajax调用中将函数传递给url字段? I want to dynamically pass a different url to the ajax call made. 我想动态地将不同的URL传递给进行的Ajax调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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