-1

$(document).ready(function () {
        $.getJSON({
            url:"https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&origin=*&srsearch=gaby&srwhat=text&srprop=snippet&format=json",
            function (result) {
                $("#display").text(result.query.search[0].title);
            }
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p id="display"></p>

Here is the $.getJSON()'s response JSON. I use result.query.search[0].title to get the title. Why #display element has nothing to show?

Gaby
  • 130
  • 1
  • 8
  • What does the debugger show at the start of the inner (succes) function? – Richard Dec 04 '16 at 07:55
  • sorry,I don't know how to debug,I'm a new fresher. But there is no error. – Gaby Dec 04 '16 at 08:00
  • Interesting. It look like a syntax error, but you really created an *object method* with name `function`. Read the `$.getJSON` documentation and a JavaScript tutorial: http://eloquentjavascript.net . – Felix Kling Dec 04 '16 at 08:06
  • RTM: http://api.jquery.com/jquery.getjson/ – mplungjan Dec 04 '16 at 08:07
  • @mplungjan one more question, how can I get the pageid in this response JSON([url](https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=meaning&srprop=size%7Cwordcount%7Ctimestamp%7Csnippet&prop=info&inprop=url))? – Gaby Dec 04 '16 at 08:19
  • 1
    A few hours focused on learning how to debug (set breakpoints, examine state, step through code) will make life much easier. – Richard Dec 04 '16 at 08:26

2 Answers2

4

Take out url: and the {} inside getJSON() method.

$(document).ready(function() {
  $.getJSON("https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&origin=*&srsearch=gaby&srwhat=text&srprop=snippet&format=json",
  function(result) {
      $("#display").text(result.query.search[0].title);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p id="display"></p>
NSTuttle
  • 3,237
  • 2
  • 17
  • 26
  • More resources on [JQuery](http://api.jquery.com/jquery.getjson/). Seeing as you are new, documentation is priceless. It may be hard to take in/understand at first, but once you learn to really break these guys down and understand all the calls... unstoppable. – NSTuttle Dec 04 '16 at 08:07
  • Yes, thanks! And, one more question, how can I get the `pageid` in this response JSON([url](https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=meaning&srprop=size%7Cwordcount%7Ctimestamp%7Csnippet&prop=info&inprop=url))? – Gaby Dec 04 '16 at 08:13
  • 1
    @Gaby: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) – Felix Kling Dec 04 '16 at 08:23
  • @FelixKling You have been a great help, thanks! – Gaby Dec 04 '16 at 08:53
2

You haven't structured your call correctly.

$.getJSON({
    url:"https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&origin=*&srsearch=gaby&srwhat=text&srprop=snippet&format=json",
    function (result) {
        $("#display").text(result.query.search[0].title);
    }
});

You appear to be using the syntax for $.ajax() which isn't compatible with $.getJSON(). Even if it was compatible, you would be missing the correct property name for the success function.

(Your syntax creates an object with a 'url' and 'function' attribute. (I'm not sure why the function gets that name.) This object is passed to $.getJSON() in the url argument. jQuery converts it to a string "[object Object]" and uses that for the url. This produces a 404. $.getJSON() fails silently if the request fails or the response is invalid.)

Instead, use:

$.getJSON(
    "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&origin=*&srsearch=gaby&srwhat=text&srprop=snippet&format=json",
    function (result) {
        $("#display").text(result.query.search[0].title);
    }
);
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
  • 1
    *"you would be missing the label for the success function."* Not sure what you mean by that, but the code is syntactically valid (unfortunately). The created object has two properties: `url` and `function` (a method). – Felix Kling Dec 04 '16 at 08:08
  • on yes! And another question, how to get the `pageid` in this response [url](https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=meaning&srprop=size%7Cwordcount%7Ctimestamp%7Csnippet&prop=info&inprop=url)? – Gaby Dec 04 '16 at 08:09
  • @FelixKling I noted the auto-naming as well while you were commenting. I'm curious about that myself. Can you point to something explaining about why that works? – Ouroborus Dec 04 '16 at 08:16
  • @Gaby There's more than one `pageid` in that response. `results.query.pages` is presented as an object. This could be iterated over via the usual means. What you're asking is not within the scope of your original question. You'd be better off submitting it as a separate question. – Ouroborus Dec 04 '16 at 08:21
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions – Felix Kling Dec 04 '16 at 08:21