Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Basic Sample Code 404 #31841

Open
Fooeybar opened this issue Jan 21, 2024 · 4 comments
Open

Fetch Basic Sample Code 404 #31841

Fooeybar opened this issue Jan 21, 2024 · 4 comments
Labels
accepting PR Feel free to open a PR to resolve this issue area: Fetch/XMLHttpRequest Content:WebAPI Web API docs effort: small This task is a small effort. goal: accuracy (Experimental label) Issues about inaccurate/incorrect content.

Comments

@Fooeybar
Copy link

Fooeybar commented Jan 21, 2024

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

What specific section or headline is this issue about?

Fetch Basic Sample Code

What information was incorrect, unhelpful, or incomplete?

The first sample given for fetch:

async function logMovies() {
  const response = await fetch("http://example.com/movies.json");
  const movies = await response.json();
  console.log(movies);
}

results in a json parse error, since the sample url returns a 404.

Recommend a sample URL which will return 200 status and sample json, such as 'https://jsonplaceholder.typicode.com/users':

async function logMovies() {
  const response = await fetch("https://jsonplaceholder.typicode.com/users");
  const users = await response.json();
  console.log(users);
}

What did you expect to see?

A successfully parsed JSON object printed to the console. This is the most basic example provided, from the source reference itself, and it should result in an error free response when copied & pasted.

Do you have any supporting links, references, or citations?

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@Fooeybar Fooeybar added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jan 21, 2024
@github-actions github-actions bot added the Content:WebAPI Web API docs label Jan 21, 2024
@bsmth
Copy link
Member

bsmth commented Jan 26, 2024

Thanks for flagging, alternatively, we could do:

// Define the function
async function logContributors() {
    const r = await fetch("https://developer.mozilla.org/en-US/docs/Glossary/TOFU/contributors.txt");
    const textData = await r.text();
    console.log(textData);
}

// Call the function
logContributors();

If we don't want to point too much traffic at jsonplaceholder.typicode.com. Note that the rest of the examples on the page point to example.com and so a lot of them will also fail, but it's possible this could be pointed out with a note that they're for illustration only.

@bsmth bsmth removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jan 26, 2024
@dipikabh dipikabh added accepting PR Feel free to open a PR to resolve this issue effort: small This task is a small effort. goal: accuracy (Experimental label) Issues about inaccurate/incorrect content. labels Apr 2, 2024
@wbamberg
Copy link
Collaborator

This is an interesting suggestion.

But even if we use https://jsonplaceholder.typicode.com/, you won't be able to send requests from developer.mozilla.org, because of d.m.o.'s CSP settings. So if a reader opens the console while reading the page and pastes examples in, it won't work. Perhaps it is even more confusing in that case, since it looks like it should work, while example.org perhaps more obviously won't?

I'm on the fence here.

Thanks for flagging, alternatively, we could do:

const r = await fetch("https://developer.mozilla.org/en-US/docs/Glossary/TOFU/contributors.txt");

Problems with this:

  • it won't work from other sites, because of CORS (so if someone tries it from example.org, it won't work).
  • it would break if we delete the page, or change the way Yari represents contributors. That URL is not designed to be a test URL for fetch demos, so there's no expectation for it to be maintained as such.
  • it only works fo GET, and only for text.The nice thing about something like https://jsonplaceholder.typicode.com/ is that we could potentially replace lots of the examples in the fetch guide, for POST, json(), etc.
@Josh-Cena
Copy link
Member

Josh-Cena commented Jun 20, 2024

Is it possible for us to host a JSON data source ourselves, a la typicode style, but with the right CSP configured on d.m.o.? Alternatively make d.m.o. accept typicode.

@wbamberg
Copy link
Collaborator

We could, but not in content of course. Also I wonder whether this site is better than https://httpbin.org/, which is what I generally use for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting PR Feel free to open a PR to resolve this issue area: Fetch/XMLHttpRequest Content:WebAPI Web API docs effort: small This task is a small effort. goal: accuracy (Experimental label) Issues about inaccurate/incorrect content.
5 participants