我有一个ajax系统设置.当MySQL查询没有返回任何数据时,我需要它来传回一个空对象.我在php脚本中创建了一个名为'data'的节点,即使查询没有返回数据,我也传递$ data ['success'] = 1.
诀窍是我无法弄清楚如何检查查询是否返回数据.
我试过了...
// sub responseObj.data for responseObj.data[0] for the following if's
if(responseObj.data[0].length == -1)
if(responseObj.data[0] == null)
if(responseObj == undefined)
//edit: added this...
if(!responseObj.data[0])
Run Code Online (Sandbox Code Playgroud)
我真的失去了我试过的任何其他各种片段的大头钉.
编辑:添加生成的xml传递给我的脚本
XML - 返回零结果
<response_myCallbackFunction>
<success>1</success>
<response_myCallbackFunction>
Run Code Online (Sandbox Code Playgroud)
XML - 返回填充的查询
<response_myCallbackFunction>
<data>
<random_data>this is data</random_data>
</data>
<success>1</success>
<response_myCallbackFunction>
Run Code Online (Sandbox Code Playgroud)
有没有办法检查javascript中的对象是否为空?
-谢谢
Obj.hasOwnProperty('blah') 似乎无法检查属性是否存在.
function isEmptyObj(obj){
for(var i in obj){
return false;
}
return true;
}
isEmptyObj({a:1}); //returns true
isEmptyObj({}); //returns false
Run Code Online (Sandbox Code Playgroud)