我在单击按钮时使用 jquery 的 .ajax()
方法。
我想知道是否有办法可以使用我在 success() 函数中 AJAX 调用的数据部分中传递的数据。
这是我的代码,
$.ajax({
url: //my URL here..
type: 'POST',
data:{
projDets : projDetailsArray,
},
datatype:'html',
error: function(){
alert('Error loading Project Information');
},
success: function(html){
//I wanted to re-use 'projDets' that I'm passing in the
//'data' part of my code..
}
});
任何帮助将不胜感激。
谢谢
最佳答案
您可以将 $.ajax
参数包装在闭包中,将“data”值设置为局部变量,然后在“data”值和“success”内部引用它“功能:
$.ajax(function() {
var data = {
projDets: projDetailArray
// ...
};
return {
url: // your URL here..
type: 'POST',
data: data,
datatype:'html',
error: function(){
alert('Error loading Project Information');
},
success: function(html){
// just reference "data" here!
}
};
}());
关于javascript - 重用 jquery-ajax 调用中传递的 'data',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247022/