jquery - Why cant null be returned to a $.getJSON() function? -
given following controller method
public actionresult testjsonresult() { return json(true, jsonrequestbehavior.allowget); // return json(null, jsonrequestbehavior.allowget); // return null; } and client side script
var url = '@url.action("testjsonresult", "test")'; $.getjson(url, function (result) { if (result) { console.log('returned result'); } else { console.log('returned null'); // never hit } }).fail(function (result) { console.log('failed'); // hit return json(null); , return null; // result status 200 ok }); then outputs 3 return value options are
return json(true, jsonrequestbehavior.allowget); // outputs "returned result" return json(null, jsonrequestbehavior.allowget); // outputs "failed" return null; // outputs "failed" acording specification
values
a json value must object, array, number, or string, or 1 of following 3 literal names: false null true
if json value can null, why else block never hit when return value null , failing of json() method or null value cannot represented json when returned getjson() method?
note, using $.ajax() without specifying datatype: hit else block when controller method return null; (i assume because return value interpreted other json).
note: using mvc-4 , jquery-1.9.1
because return json(null, jsonrequestbehavior.allowget) return empty string. , empty string not valid json string. $.getjson failed. can use javascript try json.parse("") throw error.
Comments
Post a Comment