If I return nothing to the following post, I get an AJAX parse error:
$.post('user.php', {id: 'job'}, function(data) {
console.log(data.length);
},'json');
How can I avoid it? I want to avoid the server giving any empty answers (like “0”) to limit bandwidth consumption (mobile app).
Thanks
Supporting empty responses to calls for JSON
Remove response type declaration if you know it will differ in the code you have given:
jQuery will give you decoded JSON in
data, if it receives correct JSON.Saving bandwidth
You are trying to save bandwidth by removing 2 chars (like “
{}” or “[]“) from the response. This may be pointless (not only because of specific carrier’s way of measuring bandwidth usage), as there is also the data sent not in the body of the response, but in its headers. Headers contain a lot of information, which includes:And every header is in the following format:
thus limiting number of headers sent may be orders of magnitude more efficient than avoiding two mentioned chars in the responses.