hasOwnProperty 不起作用

24s*_*ron 1 javascript json http angularjs

{error:true}我从服务器获得了一个 JSON 对象 ( )。

我尝试检查对象是否包含键“error”,并且键存在,函数 hasOwnProperty 返回false

这是我的代码:

$http({
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
        url: '/Modules/Partners/Mailing/SendMail.ashx',
        data: $.param({ contact: JSON.stringify(contact), body: partnerObject.mailTemplate.longValue, title: "" }),
        method: 'POST'
    })
    .success(function (data, status, headers, config) {
        console.log(data);
        console.log(data.hasOwnProperty('error'));

       if (data.hasOwnProperty('error')) {
           deferred.reject(contact);
       } else {
           deferred.resolve(contact);
       }
       //console.log(data)

    })
    .error(function (data, status, headers, config) {
        deferred.reject(contact);
    });
Run Code Online (Sandbox Code Playgroud)

在控制台中,我可以看到该对象包含返回的“error”hasOwnProperty('error')false

在此输入图像描述

mic*_*ael 6

我认为问题出在您收到的 JSON 对象上。其实关键不是error但是'error'。尝试看看是否data.hasOwnProperty("'error'")有效。