0

I'm new to jQuery and I was wondering if I am passing my JSON data correctly into the function.

If I am saying something incorrectly or there is more information I need to provide. Please feel free to let me know! Thank you for all the help! Much appreciated!

According to Chrome Developer Tools. I get this error:

    Uncaught TypeError: Cannot read property 'GhStatus' of undefined 

Here is my function:

        function MachineOffChecker() {
                var url = '@Html.Raw(Url.Action("index","GhCs"))';
                $.get(url, window.setInterval(
                                function(data) {                                                        
                                    if (data.GhStatus == 0) {                                                  
                                        $('#GhCsStatus_CS').buttonMarkup({ icon: 'myapp-cs' });     
                                       alert('crash');
                                    }
                                    else {
                                        $('#GhCsStatus_GH').buttonMarkup({ icon: 'myapp-gh' });      
                                        alert('running');
                                    }
                                    if (data.CsStatus == 0) {                                                 
                                        $('#GhCsStatus_CS').buttonMarkup({ icon: 'myapp-cs' });     
                                        alert('crash');
                                    }
                                    else {
                                        $('#GhCsStatus_GH').buttonMarkup({ icon: 'myapp-gh' });     
                                        alert('running');
                                    }
                                }, 2000), "json");                                                  
            }

This is my JSON data:

    {
        "GhStatus": 0,
        "CsStatus": 0
    }
Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • 2
    window.setInterval where you used it doesn't make sense. $.get expects a function, an object, an array, or a string as it's 2nd argument. you gave it an integer. – Kevin B Sep 12 '13 at 17:24
  • 1
    @tymeJV - no it does not! It would call the ajax method, but not as intended. – adeneo Sep 12 '13 at 17:24
  • 1
    @adeneo -- Just making sure...never seen the `window.setInterval` used where it is. – tymeJV Sep 12 '13 at 17:25
  • 1
    The setInterval is a function, so it would work, but it would keep calling that function over and over, which makes no sense, – adeneo Sep 12 '13 at 17:26
  • @adeneo the specific function im doing uses setInterval so I need it haha – Liondancer Sep 12 '13 at 17:34
  • @KevinB my second argument is a function. My first argument in .get() is a string (url) – Liondancer Sep 12 '13 at 17:41
  • 1
    Your second argument is the integer that is returned by `window.setInterval()`. Try it yourself. `alert(window.setInterval(function(){},1000));` it's an integer, not a function. – Kevin B Sep 12 '13 at 17:42

2 Answers2

1

You should set your dataType to json. Or you can use $.getJSON().

Edit:

You need the quotes "json"

Vlad
  • 978
  • 6
  • 13
1

Have you tried this?

 window.setInterval(
     $.get(url,function(data) {                                                        
        if (data.GhStatus == 0) {                                                  
           $('#GhCsStatus_CS').buttonMarkup({ icon: 'myapp-cs' });     
           alert('crash');
        }else{
           $('#GhCsStatus_GH').buttonMarkup({ icon: 'myapp-gh' });      
           alert('running');
        }

        if (data.CsStatus == 0) {                                                 
           $('#GhCsStatus_CS').buttonMarkup({ icon: 'myapp-cs' });     
           alert('crash');
        }else{
           $('#GhCsStatus_GH').buttonMarkup({ icon: 'myapp-gh' });     
           alert('running');
        }
     }, "json")
 ,2000);