![]() |
|
Getting Json request to work - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Coding (https://sinister.ly/Forum-Coding--71) +--- Thread: Getting Json request to work (/Thread-Getting-Json-request-to-work) Pages:
1
2
|
Getting Json request to work - hacxx - 04-09-2018 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 RE: Getting Json request to work - drxddock - 04-09-2018 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"]);
> 2162875RE: Getting Json request to work - Pikami - 04-09-2018 (04-09-2018, 02:00 PM)hacxx Wrote: 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:
RE: Getting Json request to work - hacxx - 04-09-2018 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=1Thanks RE: Getting Json request to work - Pikami - 04-09-2018 (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. Show us the code that didn't work. RE: Getting Json request to work - hacxx - 04-09-2018 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>RE: Getting Json request to work - Pikami - 04-09-2018 (04-09-2018, 06:02 PM)hacxx Wrote: I tried different variations and it didn't show anything in the screen change Code: document.write(''+ myObj.[x].ip +'<br>');Code: document.write(''+ myObj[x]["ip"] +'<br>');RE: Getting Json request to work - hacxx - 04-09-2018 Nope it doesn't show anything. RE: Getting Json request to work - drxddock - 04-09-2018 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? RE: Getting Json request to work - Pikami - 04-09-2018 (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 |