Sourav Das
Sourav Das

Reputation: 1008

Reading Data from $.getJSON Method

I am trying to consume a REST API for which i am using the $.getJSON method. I am getting the required JSON Output, but not in the method's output(in result variable) to process. Also the alert message is not coming. Its just blank output. Please find the attached code and image(Containing the response output which i need in the Developer Tools of the browser)https://i.sstatic.net/9m2VQ.png

<body>
<div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <button onclick="callSAPRestService2()">Click me</button>
    <script>

         function callSAPRestService2() {
              $.getJSON('http://xxxx:8000/sap/bc/zrest_demo?sap-client=200',
                            function (result) {
                                alert(JSON.stringify(result));
                            });
         }
    </script>
</div>

Upvotes: 1

Views: 326

Answers (1)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29693

Basically its because you have to return Json from your API response and then you can use $.getJSON. Since you haven't added your API code, I believe it isn't returning JSON response. If it is not JSON response then you can use $.get is fine.

Upvotes: 1

Related Questions