Checking for Null Is Not Working for Ajax Json Data

Checking for null is not working for ajax json data

You have to check null first then check for length

if (allotmentDetailsArray == null || allotmentDetailsArray.length == 0)
{
////
}

jQuery Ajax Json response - check if is null

Try below if will check various condition

   if((response && jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response[0])):response){

//console.log("adfdsaf");
}

if it will check if
response is null or undefined
or response == []
or response == [{}] or response[""]

I think this is the problem

    success: function(response) {
if((response && jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response[0])):response){
('#a1').append('No data');
}else{
date = response[0].Timestamp;
$('#a1').append(date);

}
};
},

hopefully it will work

if json solr response is undefined or null in ajax call

The error complains that jsonData[0] is undefined, but you're checking strLocation[0].

if (jsonData[0] === undefined)

.. or probably better:

if (jsonData.length === 0) {
...
}

Since that actually expresses what you're doing (checking if 0 results were returned), instead of using undefined to check if an array index exists.

Check if AJAX response data is empty/blank/null/undefined/0

The following correct answer was provided in the comment section of the question by Felix Kling:

if (!$.trim(data)){   
alert("What follows is blank: " + data);
}
else{
alert("What follows is not blank: " + data);
}

JSON ajax returning null value

Since Response is a custom class im guessing the problem you are having is you havent assigned a value, you just created the object instance. Unles you assigned a value on the default constructor here. you just made an instance out of it

Response answer = new Response();

You are setting no value. for testing purposes try something like this

public @ResponseBody Map<String,String> addAnsw(HttpServletRequest request, HttpServletResponse response) {

and then assign a value

Map<String,String> test = new HashMap<String,String>();
test.put("answer","hi");
return test;

if you want your Response to work you can put a value in answer on the default constructor. or create a custom constructor. Or provide a setter to be able to set the value in

public Response() {
this.answer= "My Cool answer"; // Asuming answer is a String
}

To Recieve data from the client in json you can do the following

@RequestMapping(value = "/add",method=RequestMethod.POST)
public Response addAnsw(@RequestBody YourCustomClass yourRequest) {

Note that YourCustomClass has to either have all getters and setters or should be annotated accordingly so that Jackson can deserialize what you are sending to the server.

Hope it helps

Ajax if JSON value is null set to blank or n/a

Take care about escaping text item.COL1 could have "<", ">" and "&"

function myTd(textData){
return $('<td>').text(textData==null ? 'N/A' : textData);
}

var htmlToInsert = obj.map(function (item){
return $('<tr>').append(myTd(item.COL1),myTd(item.COL2),myTd(item.COL3));
});

$('#tableBody').append(htmlToInsert);

javascript 'if' not catching null from ajax

Since you mentioned its type already shows as string, You can do:

if(data.trim()=='null')


Previous Answer:

When you say if data=='null' you are actually checking if data is the string null

 if(data == 'null') 

Should be

 if(!data) 

Or

if(typeof data === 'undefined')

Cannot read NULL variable in PHP after posting JSON array through .ajax()

You need to change to:

data: JSON.stringify(arr),

When you give an object to the data: option, it converts it to URL-encoded format, not JSON.

Or you can leave the data: option as it is, but get rid of the contentType: option, and then you should use

$data = json_decode($_POST['a_arr'], true);


Related Topics



Leave a reply



Submit