Suganya Rajasekar Suganya Rajasekar - 6 months ago 15
jQuery Question

How to pass data's through ajax if that variable have any values

The following is my code for ajax,

$( ".location" ).on( "click", function(){
var loc = $('.location:checked').map(function(_, el) {
return $(el).val();
}).get();
if($(".hedu").is(':checked'))
{
var edu = $('.hedu:checked').map(function(_, el) {
return $(el).val();
}).get();
}
$.ajax({
url: 'search_cv_result',
type: 'GET',
data: {
edu:edu,
loc: loc,
fltr:"ftr"
},
success: function(data) {
$('.course_list').html(data);
}
});
});


Here,I pass
loc
variable and also another
edu
variable ..

The
edu
value is either null or not null.

If it is null I don't want to send that
edu
value through ajax..

I tried something like this..

data: {
if(edu != ''){
edu:edu,}
loc: loc,
fltr:"ftr"
},


But I think it's not correct method..And also it show me an error..

How should I do this..Someone help me..

Answer
    if (value === loc || loc === null) {
        var _data  = { loc: loc, fltr: 'ftr' };
    } 
    else {
        var _data = { edu: edu, loc: loc, fltr: 'ftr' };
    }

    $.ajax({
       url: 'search_cv_result',
        type: 'GET',
        data: _data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert('In Ajax');
        }
    });
Comments