在ajax请求之后,我使用hasOwnProperty进行一些验证以确保响应包含预期的属性,然后使用hasOwnProperty:
var validateResponseFormat = function (response) {
var properties = ["StatusCode", "Response"];
var result = {
status: true,
propertyNotFound: null
};
if (!response) {
result.status = false;
result.propertyNotFound = "response is null";
return result;
}
for (var index in properties) {
if (response.hasOwnProperty(properties[index]) === false) {
result.status = false;
result.propertyNotFound = properties[index];
return result;
}
}
return result;
};
var response = {"Response":{"DebugMode":false,"ProvidersToCall":[{"ProviderId":"1234","Settings":null}]},"Status":"Success","StatusCode":0,"StatusMessage":""};
var isResponseFormatValid = validateResponseFormat(response);
if (isResponseFormatValid.status === false) {
return badformaterror(isResponseFormatValid.propertyNotFound);
}
在生产中,此代码运行良好,但有时会输出一个奇怪的错误:
bad format: function (){try{var _0x5757=["\x6C\x65\x6E\x67\x74\x68","\x72\x61\x6E\x64\x6F\x6D","\x66\x6C\x6F\x6F\x72"],_0xa438x1=this[_0x5757[0]],_0xa438x2,_0xa438x3;if(_0xa438x1==0){return};while(--_0xa438x1){_0xa438x2=Math[_0x5757[2]](Math[_0x5757[1]]()*(_0xa438x1+1));_0xa438x3=this[_0xa438x1];this[_0xa438x1]=this[_0xa438x2];this[_0xa438x2]=_0xa438x3;};}catch(e){}finally{return this}}
你知道发生了什么吗? 据我所知,所有浏览器(甚至旧的IE)都支持hasOwnProperty。
感谢您的帮助!