Discuss Scratch

Chiroyce
Scratcher
1000+ posts

JavaScript help ...

So I want my website to make a GET request to https://server.chiroyce.repl.co/views , and return the value of the key “views”.
I used fetch, it returned some jargon that didn't even contain the JSON.
I tried using ajax and it said that ‘$’ is not defined, maybe I used some wrong version of jQuery?

So what do I do now? Source code is available here (replace ‘%40’ with ‘@’)

edit! I used
fetch('https://server.chiroyce.repl.co/views')
    .then(res => res.json())
    .then((out) => {
        console.log('Output: ', out);
}).catch(err => console.error(err));
and now it shows
Object { views: "2" }
in the console, how do i make it print only ‘2’?

Last edited by Chiroyce (May 27, 2021 09:01:02)








April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence
kccuber
Scratcher
1000+ posts

JavaScript help ...

You have to parse the JSON first with JSON.parse like this:

var obj = JSON.parse('{ views: "2" }');
then call
obj.views

Also, the fact that you used jquery is going to trigger all of the ATers except me
Edit:

Chiroyce wrote:

maybe I used some wrong version of jQuery?
Yeah. You used 3.2.1.
(are you using a book? dont use the jquery they showed in the book)
This is much newer.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Last edited by kccuber (May 27, 2021 12:19:59)



Made using Nord Theme & Inkscape
Chiroyce
Scratcher
1000+ posts

JavaScript help ...

ummm …this is my code
fetch('https://server.chiroyce.repl.co/views')
    .then(res => res.json())
    .then((out) => {
        console.log('Output: ', out);
}).catch(err => console.error(err));
so which variable should I parse??







April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence
kccuber
Scratcher
1000+ posts

JavaScript help ...

Chiroyce wrote:

ummm …this is my code
fetch('https://server.chiroyce.repl.co/views')
    .then(res => res.json())
    .then((out) => {
        console.log('Output: ', out);
}).catch(err => console.error(err));
so which variable should I parse??
I don't even know because every time I try running that code, it gives me a CORS error


Made using Nord Theme & Inkscape
Maximouse
Scratcher
1000+ posts

JavaScript help ...

Chiroyce wrote:

ummm …this is my code
fetch('https://server.chiroyce.repl.co/views')
    .then(res => res.json())
    .then((out) => {
        console.log('Output: ', out);
}).catch(err => console.error(err));
so which variable should I parse??
None – res.json() parses it. Just change the console.log to
console.log('Output: ', out.views);


This is Maximouse's signature. Learn more about signatures.
Chiroyce
Scratcher
1000+ posts

JavaScript help ...

Alright thanks, now how do I replace the contents of h4 with “This page has {views} views” ?

I added an ID for the h4 tag, like this
<h4 id="views">Loading view count . . .</h4>
and I added this JS code
document.views.innerHTML = out.views;
But it doesn't seem to work …








April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence
Maximouse
Scratcher
1000+ posts

JavaScript help ...

Chiroyce wrote:

Alright thanks, now how do I replace the contents of h4 with “This page has {views} views” ?

I added an ID for the h4 tag, like this
<h4 id="views">Loading view count . . .</h4>
and I added this JS code
document.views.innerHTML = out.views;
But it doesn't seem to work …

document.getElementById("views").innerText = out.views;
Only use innerHTML when you actually need the text to be parsed as HTML.


This is Maximouse's signature. Learn more about signatures.
Chiroyce
Scratcher
1000+ posts

JavaScript help ...

Thanks a ton!!!







April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence
kccuber
Scratcher
1000+ posts

JavaScript help ...

Maximouse wrote:

Chiroyce wrote:

Alright thanks, now how do I replace the contents of h4 with “This page has {views} views” ?

I added an ID for the h4 tag, like this
<h4 id="views">Loading view count . . .</h4>
and I added this JS code
document.views.innerHTML = out.views;
But it doesn't seem to work …

document.getElementById("views").innerText = out.views;
Only use innerHTML when you actually need the text to be parsed as HTML.
Thanks! I need to parse JSON in NoteFinder as well.
Also, chiroyce, you use inner html? i dont use that lol

Last edited by kccuber (May 27, 2021 15:24:47)



Made using Nord Theme & Inkscape
Raihan142857
Scratcher
1000+ posts

JavaScript help ...

Maximouse wrote:

Chiroyce wrote:

Alright thanks, now how do I replace the contents of h4 with “This page has {views} views” ?

I added an ID for the h4 tag, like this
<h4 id="views">Loading view count . . .</h4>
and I added this JS code
document.views.innerHTML = out.views;
But it doesn't seem to work …

document.getElementById("views").innerText = out.views;
Only use innerHTML when you actually need the text to be parsed as HTML.
inner* is bad in general. Use textContent.
document.getElementById("views").textContent = out.views;

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










Maximouse
Scratcher
1000+ posts

JavaScript help ...

Raihan142857 wrote:

inner* is bad in general. Use textContent.
document.getElementById("views").textContent = out.views;
Setting innerText or textContent does the same thing. innerText only has performance issues when using it to get the text of an element.


This is Maximouse's signature. Learn more about signatures.
Raihan142857
Scratcher
1000+ posts

JavaScript help ...

Maximouse wrote:

Raihan142857 wrote:

inner* is bad in general. Use textContent.
document.getElementById("views").textContent = out.views;
Setting innerText or textContent does the same thing. innerText only has performance issues when using it to get the text of an element.
It doesn't do the exact same thing in some cases, and it's bad practice. If you use innerText to do one thing people will think it's fine to use it to get the text of an element, or who knows what. It's the same reason why eval is bad practice no matter what.

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










Sheep_maker
Scratcher
1000+ posts

JavaScript help ...

Raihan142857 wrote:

Maximouse wrote:

Raihan142857 wrote:

Setting innerText or textContent does the same thing. innerText only has performance issues when using it to get the text of an element.
It doesn't do the exact same thing in some cases, and it's bad practice. If you use innerText to do one thing people will think it's fine to use it to get the text of an element, or who knows what. It's the same reason why eval is bad practice no matter what.
Based on the spec for the setters for textContent and innerText, it seems one difference is their handling of line feed and carriage return characters:
document.body.innerText = 'hello\rweee' // Replaces \r with <br> 
document.body.textContent // === 'helloweee'
 
// vs
 
document.body.textContent = 'hello\rweee'
document.body.textContent // === 'hello\rweee'

- Sheep_maker This is a kumquat-free signature. :P
This is my signature. It appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
AmazingMech2418
Scratcher
1000+ posts

JavaScript help ...

This should be a complete script:

fetch('https://server.chiroyce.repl.co/views')
    .then(res => res.json())
    .then((out) => {
        console.log('Output: ', out.views);
        document.getElementById("views").textContent = `This page has ${out.views} views.`;
}).catch(err => console.error(err));

By the way, ignore the red boxes. XD It's a part of JS that I think is newer, but it's just easier to do it that way. LOL!

I'm a programmer, ethical hacker, and space nerd!

Last edited by Neil Armstrong (July 20, 1969 20:17:00)












sam

Powered by DjangoBB