0

I am sending data to a server which returns a json response which is embedded in the html body. My intention is to extract the responses in order to

  • Save in a mysql database
  • create notifications

This is the java script I am trying to use but it keeps returning the first condition as true even if the string it's searching for doesn't exist

PAGE JAVASCRIPT

    <script>
$( document ).ready(function() {
    var text = document.body.innerHTML;

    var code_sent = '[status] =&gt; sent';
    var password_recieved = '[pw] =&gt;';

    if (text.indexOf(code_sent) >= 0) 
    {
        alert('Code Sent');
        window.location.replace("index.php");
    } 
    else if (text.indexOf(password_recieved) >= 0) 
    {
        alert('Password Recieved');
        window.location.replace("index.php");
    }
});
</script>

UPDATE: Added screenshot of response server response

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3682205
  • 241
  • 4
  • 16
  • Can you please provide value of `text`? – Deepak Biswal Nov 04 '15 at 07:50
  • 1
    Response is not JSON, its array. Double check. use stringify to convert. Instead screenshot, post your JSON code. – Vijayakumar Selvaraj Nov 04 '15 at 08:01
  • For JSON recursive search, refer this link http://stackoverflow.com/questions/9133500/how-to-find-a-node-in-a-tree-with-javascript. To search in array Indexof will help. refer here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – Vijayakumar Selvaraj Nov 04 '15 at 08:07

0 Answers0