XMLHttpRequest Error
Answered
NanotechPikachu posted this in #help-forum
Hi, I'm getting the error as in the attached pic. I'm trying to use a function to get a link of the object it returns but unfortunately it errors. And i can't understand the reason and how to rectify it.
Here's the function code i use
And heres the next page code
Please help me
Here's the function code i use
import { ANIME } from '@consumet/extensions';
const gogoanime = new ANIME.Gogoanime();
async function download(id) {
const res = await gogoanime.fetchEpisodeSources(id);
const d = res.download;
let result;
if (d) {
result = await gogoanime.fetchDirectDownloadLink(d);
} else {
result = false;
};
console.log(d, res, result)
return result;
};
export { download }And heres the next page code
"use client"
import { useSearchParams } from 'next/navigation';
import { useState, useEffect, Suspense } from 'react';
import { download } from '../functions/gogo.js';
function Download() {
const [data, setData] = useState({});
const [loading, setLoading] = useState(true);
const params = useSearchParams();
const link = params.get('link');
useEffect(() => {
if (link) {
download(link).then((ans) => {
console.log(ans);
setData(ans);
setLoading(false);
});
} else {
setLoading(false);
}
}, [link]);
const m = data;
console.log(m)
return (
<>
<div className="ml-6 mr-6 mt-6 mb-20">
{loading ? (
<div className="flex flex-grow w-full min-h-screen items-center justify-center text-center"><p>Loading...</p></div>
) : m ? (
<div>{JSON.stringify(m)}
</div>
) : (<div className="flex flex-grow w-full min-h-screen items-center justify-center text-center"><p>No results Found!</p></div>)}
</div>
</>
);
}
export default function Dwld() {
return (
<Suspense fallback={<div className="flex flex-grow w-full min-h-screen items-center justify-center text-center"><p>Loading...</p></div>}>
<Download />
</Suspense>
);
}Please help me
Answered by averydelusionalperson
export default function Dwld({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
return (
<Suspense fallback={<div className="flex flex-grow w-full min-h-screen items-center justify-center text-center"><p>Loading...</p></div>}>
<Download searchParams={searchParams} />
</Suspense>
);
}do something like this
53 Replies
Btw, I am hosting in vercel
that's cors error
it should be fixed on the backend side
How to exactly?
well, there are multiple ways
you add cors headers
on the backend
try to read this, maybe you'll get the gist
Hmmm, does something like adding something in vercel.json work? Cuz I tried something like that from what I got in Google earlier
I believe this is not next.js nor vercel issue
the backend is not written on next.js
and not hosted on vercel
but if you can solve it by editing vercel.json, then good for you ig
maybe share it here
{
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Access-Control-Allow-Credentials",
"value": "true"
},
{
"key": "Access-Control-Allow-Origin",
"value": "https://anidon.vercel.app"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "X-Requested-With, Access-Control-Allow-Headers, Content-Type, Authorization, Origin, Accept, Client-Security-Token, Accept-Encoding"
}
]
}
]
}This is in my vercel.json though nothing changed
@averydelusionalperson I believe this is not next.js nor vercel issue
I tried reading that, but I haven't written a backend for my web
you need to add headers on backend
not on vercel
well, the embtak API is backend
hmm, I think I got the issue
Hmm?
you're trying to fetch data from client components
do proper fetching in server component
so, I need to replace "use client" to "use server"? But then I can't use useState na?
why would you need useState tho, just use
const data = await download(link)When I tried using await earlier in useEffect, it errored. So, I was using the workaround way of useState
don't use useEffect or useState
just fetch directly
@averydelusionalperson https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating
Also, i don't think any of this helps me? I am using a package na?
So something like
import { useSearchParams } from 'next/navigation';
import { Suspense } from 'react';
import { get } from '../functions/gogo.js';
async function Anime() {
const params = useSearchParams();
const id = params.get('id');
const data = await download(id)
// console.log(!data.title);
const m = data;
const l = [];
const n = [];
return (
//Whatever here
)
}no, you can't use hooks
U mean useSearchParams?
Then how do I get params?
I need em
Sorry, i don't get it
export default function Dwld({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
return (
<Suspense fallback={<div className="flex flex-grow w-full min-h-screen items-center justify-center text-center"><p>Loading...</p></div>}>
<Download searchParams={searchParams} />
</Suspense>
);
}do something like this
Answer
Oh okay. Lemme try. So, Suspense won't be a problm right?
I don't think so
Lemme check
@NanotechPikachu you there?
Thnx man. It worked(just completed now cuz some random error popped up). And sorry for wasting your time.
no worries, if the problem is solved, consider marking the answer as solution
Original message was deleted
Okay. Will do and again, thnx