Skip to content Skip to sidebar Skip to footer

Json Response Incorrectly Matches Partial Key For Color Data

Below JavaScript code matches to 'COMPLETED' due to jsonData.search(key) !== -1 which is wrong because JSON response doesn't contain COMPLETED, it should show red color instead of

Solution 1:

This https://jsfiddle.net/xq7hLo6h/6/ fixes the issue by simply searching the JSON string for "KEY" (with quotes) instead of KEY:

if (jsonData.search("\""+ key +"\"") >=0) { /* ... */ }

This way the problem with COMPLETED being found inside "NOT_COMPLETED" is circumvented.

However a more robust way would be to actually parse the JSON string in order to get the object and then check the actual property.

Post a Comment for "Json Response Incorrectly Matches Partial Key For Color Data"