jQuery跳过可选参数

Javascript jQuery skipping optional params

本文关键字:参数 jQuery      更新时间:2023-09-26

1行JavaScript代码

$.post('/blah', { comment_id: 1, description: ... });

由于某些原因,该调用不是作为JS调用进行的,并且根据文档,post方法允许另外2个可选参数,successdataType。我想跳过success并将dataType设置为script。我试过了,它给了我一个错误

$.post('/blah', { comment_id: 1, description: ... }, ,"script");

通过nullundefined:

$.post('/blah', { comment_id: 1, description: ... }, null,"script");
// use void for undefined (because undefined can be reassigned)...
$.post('/blah', { comment_id: 1, description: ... }, void 0,"script");

您的代码是无效Javascript。如果你想跳过一个参数,你必须传递一个null值:

$.post('/blah', { comment_id: 1, description: 'Blah' }, null, "script");

默认参数可以为null