1

I got a JSON object ({error:true}) from the server.

I try to check if the object contains the key "error" and either the key exists, the function hasOwnProperty returns false.

This is my code:

$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);
    });

In the console I can see that the object contains the "error" key by the hasOwnProperty('error') returns false

enter image description here

Valijon
  • 12,667
  • 4
  • 34
  • 67
24sharon
  • 1,859
  • 7
  • 40
  • 65

6 Answers6

6

I think that the problem is with the JSON object you receive. In fact the key is not error but 'error'. Try to see if data.hasOwnProperty("'error'") works.

mic4ael
  • 7,974
  • 3
  • 29
  • 42
  • 1
    just a note that with JSON Object's you must surround the key in quotation marks not single quotes " " as opposed too ' ' Otherwise its not valid!! – Dizzy Bryan High Dec 06 '19 at 09:28
1

Your success method receives String data, not JSON:

.success(function (data, status, headers, config) {
    var result = angular.fromJson(data);

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

BTW: If it were JSON, in console, you would see:

enter image description here

Valijon
  • 12,667
  • 4
  • 34
  • 67
1

You can use in operator to check if property exist on object/array. It can be used like this "error" in data // will return true

Aditya
  • 395
  • 2
  • 6
  • Not quite; that will tell you if the object is iterable, according to https://stackoverflow.com/a/136411/8949477. – jtunhag Nov 10 '22 at 06:52
0

have you tried using if(data && data.error) instead of if (data.hasOwnProperty('error'))

It's probably because the error property is inherited. See more about hasOwnProperty and inherited properties here

cguest
  • 71
  • 5
0

check the content-type of data sent from server. it should be 'application/json' then it gets converted to javascript object implicitly. to check use "typeof data" it must be "object" else if its string then you need to parse it to object by using JSON.parse

ThatsME
  • 147
  • 7
0

I don't know why but return the object like JsonSerializer.Serialize("{\"error\":true}");

Instead of JsonSerializer.Serialize("{'error':true}"); or JsonSerializer.Serialize("{error:true}");

Resolve my problem

24sharon
  • 1,859
  • 7
  • 40
  • 65