如果条件在AJAX请求中

I have a AJAX request where I would like to pass data based on if condition.

data: testobj is a JSON object that will return {"name":"tom"}

$.ajax({
    type: "POST",
    url: 'test.asp',
    data: testobj,
    success: function(response){
         alert(response);
    }
});

the data might sometimes be undefined or null. so i'm looking to have something like this

if (testobj != undefined){
    data: testobj
}

Is this possible to have this condition without duplicating the whole block ?

Just store your config separately.

const config = { type: 'POST', url: 'test.asp', success: function () {} }
if (testObj != undefined) config.data = testObj
$.ajax(config)