0

how could I use Jquery to make a post request to the fandom's API and get data from the request's responseJSON?

When I use this code:

$.post(
"https://drednotio.fandom.com/api.php",
{
    action: "query",
    format: "json",
    list: "allpages",
    apfrom: "a",
    aplimit: "500",
    token: "fa5fed4cd6bdb8bd8b901075ec59fa39+\\",
    bot: "true",
},
function (data) {
    var response = jQuery.JSON.parse(data);

    console.log(data);
}
);

I get this error in the console:

enter image description here

I've tried using xmlhttprequest and ended up with the same results... Could someone help me out?

connexo
  • 53,704
  • 14
  • 91
  • 128
Ownslo
  • 3
  • 6
  • Is what you get is valid `JSON`? What do you see when you `console.log(data)`? – goto May 29 '20 at 14:48
  • 2
    Either use `JSON.parse` or `jQuery.parseJSON()` not like `jQuery.JSON.parse` – palaѕн May 29 '20 at 14:48
  • Hello. This is what console says https://i.imgur.com/41Xcalr.png, and I did do JSON.parse, it said the same thing. – Ownslo May 29 '20 at 14:49
  • **Show the response the browser gets.** – connexo May 29 '20 at 14:53
  • https://i.imgur.com/5FKYxof.png response and error, using the code. – Ownslo May 29 '20 at 14:54
  • 2
    jQuery will parse the response from JSON into a JavaScript object. You get the error because you are trying to parse a JavaScript object as if it were a JavaScript string containing JSON. – Quentin May 29 '20 at 14:55
  • What? Dude that's no help. – Ownslo May 29 '20 at 15:02
  • @Ownslo read the comment by @Quentin, you don't need to do `jQuery.JSON.parse` since it's already a `JavaScript` object, `jQuery` automatically does that for you and returns the proper `JavaScript` object. Just access whatever properties you need - `data.whatever`... – goto May 29 '20 at 18:26

0 Answers0