简体   繁体   English

jQuery Ajax调用函数

[英]jQuery Ajax Call function

I want to be able to call a function from the "on success" region instead of having to place my code in that region. 我希望能够从“成功”区域中调用函数,而不必将我的代码放在该区域中。 I'll be using the code twice so I'm trying to figure out how to place it outside of the jQuery.ajax() function. 我将使用两次代码,所以我试图找出如何将其放置在jQuery.ajax()函数之外。

Here is my current code: 这是我当前的代码:

$.ajax({
  type: "GET",
  url: "ws/getweather.ashx?zip=" + vZip,
  dataType: "xml",
  success: function (xml) {
  $(xml).find('weather').each(function () {
     // Load New Data
     ...
});
},
  error: function (xml) {
    alert("Unrecognized Region. Please try again.");
  }
});       

So instead of having... 所以不用...

function (xml) {
  $(xml).find('weather').each(function () {
     // Load New Data
     ...
});

I'd like to put the name of another function, and pass the xml to that function. 我想输入另一个函数的名称,并将xml传递给该函数。 That way I can have other events call the same set of code. 这样,我可以让其他事件调用相同的代码集。

Thanks in advance! 提前致谢!

UPDATE=================================================== UPDATE ================================================== ==

Thanks to Mike Richards for his timely response. 感谢Mike Richards的及时回应。 I'm including the exact syntax below because I had to add a few details to make it work...meaning, pass the XML to the other function. 我将在下面包含确切的语法,因为我必须添加一些细节才能使其正常工作……意味着,将XML传递给其他函数。

$.ajax({
type: "GET",
url: "ws/getweather.ashx?zip=32751",
dataType: "xml",
success: function (xml){
    weatherize(xml);
},
error: function (xml) {
    alert("Unrecognized Region. Please try again.");
}
});

And then somwhere below, my other function 然后在下面的其他地方,我的其他功能

function weatherize(xml) {
  $(xml).find('weather').each(function () {
// Load New Data
...
})
};

you can just pass in a function for that parameter :) 您可以只为该参数传递一个函数:)

success : successFunction,

and then, somehwere else: 然后是:

function successFunction(data) {
  // Do Something
}

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

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