Тема: Help with get request in Json format

Hi,

I'm trying to implement a feature in a program but i'm having troubles fetching the JSON url.

The url is
http://api.stopforumspam.org/api?json&a … l@here.com

And the code received should be something like this (the "var json" in this case is just an example)

var json = '{"success":1,"email":{"frequency":1,"appears":1,"confidence":0.03}}',
    obj = JSON && JSON.parse(json) || $.parseJSON(json);

document.write('Is a Spammer? '+obj.email.appears+'<br>');
document.write('Last Seen: '+obj.email.lastseen+'');

Also is it possible to change 1 or 0 in obj.email.appears to yes or no?

Thanks

2

Re: Help with get request in Json format

So you just don't know how to get the JSON data through the url?

Also is it possible to change 1 or 0 in obj.email.appears to yes or no?

yeah

document.write('Is a Spammer? '+(!!obj.email.appears?'yes':'no')+'<br>');
All you want is a dingle,
What you envy's a schwang,
A thing through which you can tinkle,
Or play with, or simply let hang...

3

Re: Help with get request in Json format

So you just don't know how to get the JSON data through the url?

Yes

4

Re: Help with get request in Json format

Okay, if you use jQuery, you can do it like this
https://codepen.io/anon/pen/EbGagq

$.ajax('https://jsonplaceholder.typicode.com/posts/1')
.then((result) => { 
  console.log(result); 
  console.log('body:' + result.body); 
  console.log('id: ' + result.id);
  console.log('title: ' + result.title); 
  console.log('userId: ' + result.userId);
});

if you don't use jQuery, just google for

js ajax request

All you want is a dingle,
What you envy's a schwang,
A thing through which you can tinkle,
Or play with, or simply let hang...

5 Востаннє редагувалося leofun01 (30.11.2017 23:37:42)

Re: Help with get request in Json format

Open page
https://www.w3schools.com/js/tryit.asp? … yjson_ajax
in your browser and replace code by this:

<!DOCTYPE html>
<html>
<body>
<h2>XMLHttpRequest + JSON</h2>

<script>
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.body.innerHTML += '<pre>' + this.responseText + '</pre>';
      var myObj = JSON.parse(this.responseText);
      document.body.innerHTML += '<p>' + myObj.name + '</p>';
      document.body.innerHTML += '<p>' + myObj.age + '</p>';
      document.body.innerHTML += '<p>' + myObj.pets[1].name + '</p>';
    }
  };
  xhttp.open("GET", "https://www.w3schools.com/js/json_demo.txt", true);
  xhttp.send();
</script>

</body>
</html>

and press "Run".

It's very simple example that will show you how it work.
javascript.enabled = true (required).

6

Re: Help with get request in Json format

Be careful with parameters.
xhttp.open("GET", "/url/", true);
here true value as last parameter mean that this request has been sent asynchronously.

7

Re: Help with get request in Json format

I moved this topic into Just English forum. Next time create your questions just in this forum (not topic).
No sense trash topic about English in Ukrainian language, what i moved from this topic.

Говорила баба діду: «Я поїду к Білодіду, Ізучу двомовну мову І вернусь обратно знову». А дід бабі: «Не *изди, К Білодіду нєт їзди, — Туди не ходять поїзди»
Подякували: FakiNyan, leofun012

8

Re: Help with get request in Json format

@FakiNyan & @leofun01

Thanks

Подякували: 0xDADA11C71