Login Register


Getting Json request to work filter_list
Author
Message
Getting Json request to work #1
Hi,

I want to provide to users a simple method to check if they have currently succefully signup.
To achieve this i'm using a json request that will get the page and show only the elements i want.

Below is a generic json request script that i have use in the past for lists.
Code:
<script> var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var myObj = JSON.parse(this.responseText); document.write('<center><table><tr><td>'); for (x in myObj.matches) { document.write(''+ myObj.matches[x].ip_str +'<br>'); } document.write('</td></tr></table></center>'); } }; xmlhttp.open("GET", "https://www.example.com", true); xmlhttp.send(); </script>

I want to use the code above to get the code below and only show some elements

Code:
[{"banner_type":null,"id":"2162875","campid":"8909","ip":"154.69.47.83","subid":"","subid2":"","subid3":"","earn":"300.0","ddate":"2018-04-05","dtime":"02:36:11","ustamp":1491377771,"preid":null,"gateid":"1310615","country":"US","currency":"USD","banner_id":"0","type":"CPC"}, {"banner_type":null,"id":"2155364","campid":"10357","ip":"111.125.111.139","subid":"","subid2":"","subid3":"","earn":"0.80","ddate":"2018-04-04","dtime":"08:29:26","ustamp":1491312566,"preid":null,"gateid":"1310615","country":"PH","currency":"USD","banner_id":"0","type":"CPC"}]

I just want to show the user some elements like ip, earn, ddate and country.

Thanks

Reply

RE: Getting Json request to work #2
Since you've already parsed the response text as JSON, you can access any JSON value you want through the myObj variable. For example,
Code:
console.log(myObj["id"]); > 2162875

Reply

RE: Getting Json request to work #3
(04-09-2018, 02:00 PM)hacxx Wrote:
Code:
[{"banner_type":null,"id":"2162875","campid":"8909","ip":"154.69.47.83","subid":"","subid2":"","subid3":"","earn":"300.0","ddate":"2018-04-05","dtime":"02:36:11","ustamp":1491377771,"preid":null,"gateid":"1310615","country":"US","currency":"USD","banner_id":"0","type":"CPC"}, {"banner_type":null,"id":"2155364","campid":"10357","ip":"111.125.111.139","subid":"","subid2":"","subid3":"","earn":"0.80","ddate":"2018-04-04","dtime":"08:29:26","ustamp":1491312566,"preid":null,"gateid":"1310615","country":"PH","currency":"USD","banner_id":"0","type":"CPC"}]

I just want to show the user some elements like ip, earn, ddate and country.

So in your example you have 'myObj' witch holds your data, according to your JSON this object is an array.
So lets say you want to get the ip address of the first array element, so the syntax would be this 'myObj[0]["ip"]'
Basically you have to chose what element you want.
Here's a picture that might help you understand better:
[Image: 2eabBZ7.png]

Reply

RE: Getting Json request to work #4
I tried both answers but didn't work. Here is the link where the Json data is fetch.

Code:
https://www.cpalead.com/dashboard/reports/leads_api.php?id=210923&token=042A598ADFC1A4FBEC&cpc=1

Thanks
(This post was last modified: 04-09-2018, 04:25 PM by hacxx.)

Reply

RE: Getting Json request to work #5
(04-09-2018, 04:25 PM)hacxx Wrote: I tried both answers but didn't work. Here is the link where the Json data is fetch.

Code:
https://www.cpalead.com/dashboard/reports/leads_api.php?id=210923&token=042A598ADFC1A4FBEC&cpc=1

Thanks

Show us the code that didn't work.

Reply

RE: Getting Json request to work #6
I tried different variations and it didn't show anything in the screen

Here is the latest test

Code:
<script> var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var myObj = JSON.parse(this.responseText); document.write('<center><table><tr><td>'); for (x in myObj) { document.write(''+ myObj.[x].ip +'<br>'); } document.write('</td></tr></table></center>'); } }; xmlhttp.open("GET", "https://www.cpalead.com/dashboard/reports/leads_api.php?id=210923&token=042A598ADFC1A4FBEC&cpc=1", true); xmlhttp.send(); </script>

Reply

RE: Getting Json request to work #7
(04-09-2018, 06:02 PM)hacxx Wrote: I tried different variations and it didn't show anything in the screen

Here is the latest test

Code:
<script> var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() {    if (this.readyState == 4 && this.status == 200) {        var myObj = JSON.parse(this.responseText);      document.write('<center><table><tr><td>');      for (x in myObj) {      document.write(''+ myObj.[x].ip +'<br>'); }      document.write('</td></tr></table></center>'); } }; xmlhttp.open("GET", "https://www.cpalead.com/dashboard/reports/leads_api.php?id=210923&token=042A598ADFC1A4FBEC&cpc=1", true); xmlhttp.send(); </script>

change
Code:
document.write(''+ myObj.[x].ip +'<br>');
to
Code:
document.write(''+ myObj[x]["ip"] +'<br>');
and try again
(This post was last modified: 04-09-2018, 06:23 PM by Pikami.)

Reply

RE: Getting Json request to work #8
Nope it doesn't show anything.

Reply

RE: Getting Json request to work #9
There could be an issue with the same origin policy. The policy is implemented in most or all modern web browsers as a security measure -- basically, if you are on any domain other than the one you're trying to request data from (www.cpalead.com), then it won't let you. In other words, if you are trying to host that script on any website other than cpalead.com, then it won't work. Is that the case?

Reply

RE: Getting Json request to work #10
(04-09-2018, 08:32 PM)0xAeschylus Wrote: There could be an issue with the same origin policy. The policy is implemented in most or all modern web browsers as a security measure -- basically, if you are on any domain other than the one you're trying to request data from (www.cpalead.com), then it won't let you. In other words, if you are trying to host that script on any website other than cpalead.com, then it won't work. Is that the case?

It won't be the case, I used this API in my site 2 weeks ago: http://quotes.rest/
It worked, but I used fetch() instead of XMLHttpRequest

Reply







Users browsing this thread: 1 Guest(s)