-5

how i can compare two values json return true in is delivered property and i want to compare it with string

$.getJSON('/SalesInvoice/GetSI/' + $('#SearchSI').val(), function (data) {

     if (data.ex == "OK") {
         if(data.Voucher.IsDelivered == "true")
         {

             alert('hi');
         };
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Agahii
  • 39
  • 1
  • 8
  • 2
    Uh, so the comparison failed, maybe you should log the values and figure out what they are then ? – adeneo Jul 02 '14 at 15:58
  • What's data? What does GetSI do? this does not contain the bear minimum of information to answer this question – Liam Jul 02 '14 at 15:59
  • Also see [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Jul 02 '14 at 15:59

2 Answers2

0

If in the JSON, the value is true and you want to check it using "true" it would result in false.

I think you should convert the string to a simple bool true.

if(data.Voucher.IsDelivered == true)
{
   alert('hi');
};

Or a simpler one

if(data.Voucher.IsDelivered)
{
   alert('hi');
};

Because if you see, the (bool == string) = false. Always check the data types while using a conditional statement.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
0

Why dont you add an else to the IF statement so that you can see exactly what is in the variable in question. Like this:

 if (data.ex == "OK") {
     if(data.Voucher.IsDelivered == "true")
     {

         alert('hi');
     } else {
         alert( data.Voucher.IsDelivered + ' = ' + typeof data.Voucher.IsDelivered )
     }
AShepherd85
  • 65
  • 1
  • 5