这里我想做的变量不是空的,我想在数据中传递一个变量,在这种情况下,区域是空的,所以我想传递city和listing类型的参数,但我不知道怎么办?
var city = "Karnadaka";
var area = "";
var listing type = "RENT";
$.ajax({
type: 'GET',
url: "http://www.domain.com/api/get/searchProperties",
data: {
area: area,
city: city,
listingType: listing_type
},
success: function(data) {
console.log(data);
}
});
答案 0 :(得分:1)
您可以使用delete
删除对象
var city = "Karnadaka";
var area = "";
var listing_type = "RENT";
var data={
area: area,
city: city,
listingType: listing_type
}
for (k in data){
if(data[k]=="") delete data[k];
}
$.ajax({
type: 'GET',
url: "http://www.domain.com/api/get/searchProperties",
data: data,
success: function(data) {
console.log(data);
}
});
答案 1 :(得分:0)
试试这个:
var city = "Karnadaka";
var area = "";
var listing_type = "RENT";
var data = {};
if(city != '')
data['city'] = city;
if(area != '')
data['area'] = area;
if(listing_type != '')
data['listing_type'] = listing_type;
$.ajax({
type: 'GET',
url: "http://www.domain.com/api/get/searchProperties",
data: data,
success: function(response) {
console.log(response);
}
});