在Jquery中的xhrFields中添加函数

时间:2012-10-25 08:41:19

标签: jquery

是否可以在xhrFields中添加if条件:在jquery中 - ajax调用?

前:

xhrFields : { 
   if(flag =='Y'){
   withCredentials: true 
   }
 }

2 个答案:

答案 0 :(得分:1)

尝试:

xhrFields: {
    withCredentials: flag == 'Y'
}

但是,如果您确实需要在flag!='Y'时完全省略该选项,则可以执行以下操作:

xhrFieldsObj = {
   /* You can put all the static options in here */
};
if (flag == 'Y') {
    xhrFieldsObj.withCredentials = true;
};
...
   xhrFields: xhrFieldsObj

答案 1 :(得分:1)

var xhrFields = {};

if (flag == 'Y') {
    xhrFields.withCredentials = true;
}