这个对象是空的吗,Javascript
我已经安装了一个ajax系统。当MySQL查询没有返回任何数据时,我需要它返回一个空对象。我在php脚本中创建了一个名为“data”的节点,即使查询没有返回任何数据,我也会传递$data['success']=1 诀窍是我不知道如何检查查询是否返回了数据 我试过这个对象是空的吗,Javascript,javascript,Javascript,我已经安装了一个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(resp
// 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])
我真的失去了我尝试过的任何其他片段
编辑:添加传递给我的脚本的生成的xmlXML-返回零结果
<response_myCallbackFunction>
<success>1</success>
<response_myCallbackFunction>
1.
XML-返回填充的查询
<response_myCallbackFunction>
<data>
<random_data>this is data</random_data>
</data>
<success>1</success>
<response_myCallbackFunction>
这是数据
1.
是否有办法检查javascript中的对象是否为空? -谢谢你可以试试
if( responseObj["data"] ) {
// do stuff with data
}
或
Obj.hasOwnProperty('blah')
似乎无法检查该属性是否存在
function isEmptyObj(obj){
for(var i in obj){
return false;
}
return true;
}
isEmptyObj({a:1}); //returns true
isEmptyObj({}); //returns false
如果
responseObj
是XML文档对象(来自xhr.responseXML
属性),则:
如果
responseObj
是JavaScript对象:
if (responseObj.data) {
// do stuff...
}
对于ES5,您有
getOwnPropertyNames
:
var o = { a:1, b:2, c:3 };
Object.getOwnPropertyNames(o).length // 3
你能从你的php中为你返回的内容添加一个代码片段吗。。。我原以为responseObj.hasOwnProperty(“数据”)会这么做。。。但是不行。。。格里拉。。。我刚刚读到Obj.hasOwnProperty(“property”)无法检查属性的存在。()不知道你在找什么。。。hasOwnProperty()可以检查属性(因此而得名)是否存在。建议不要在about.com上阅读javascript:Dsure,Mozilla开发者中心有一些很棒的文档:
if (responseObj.data) {
// do stuff...
}
if(typeof responseObj.data != 'undefined') {
// code goes here
}
var o = { a:1, b:2, c:3 };
Object.getOwnPropertyNames(o).length // 3