Search code examples
javascriptjqueryjsonjsonp

Access JSON data inside brackets - home({"a": { "prop1": "data", "prop2": "data"});


I'm getting some result when calling to some service as below.

Call:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script>
    function getData() {    

        var clientID = '123';
        var pageName = 'home';
        var serviceURL = 'https://service.com/meta?_callback=home&_o=' + clientID + '&_t=' + pageName + '';     

        $.ajax({
            url: serviceURL,
            method: 'GET',
            aynch:true,
            success: function (data) {   
                console.log(data);
            },
            error: function (data, textStatus, errorThrown) {
                alert('Service Call Error '+ errorThrown);
            }
        });
    }
</script>

Response:

home({
      "a": {
       "prop1": "data",
        "prop2": "data"
      },
      "b": {
        "prop1": "data",
        "prop2": "data"
      }
});

How to access data of this result using JavaScript ?  


Solution

  • This looks a lot like a JSONP response!

    Have a read on what is JSONP (http://en.wikipedia.org/wiki/JSONP)

    <!-- call api -->
    <script src="http://api.url.com?callback=home"></script>
    <script>
         // handle response
         function home(data) {
             console.log(data);
         }
     </script>