-1

I need to load page from this url: https://medalist-a805c.web.app/HeardSuggest_Medalist.html which actually loads a page from a express server. I'm able to use cors, the express middleware. But the problem is, even though i need to load this url: https://www.alwaysheard.com/embed/Medalist/brand/$2a$10$Z9ib22YZyRWWZz/Vlzju3u9eZCZM.a8oUYorNPsKGzLPKu6vM696K?uname=embed&orgin=https%3A%2F%2Fwww.alwaysheard.com eventually.

The weird problem i'm facing is it always returns the homepage rather than the url i am trying to get which is: https://www.alwaysheard.com/embed/Medalist/brand/$2a$10$Z9ib22YZyRWWZz/Vlzju3u9eZCZM.a8oUYorNPsKGzLPKu6vM696K?uname=embed&orgin=https%3A%2F%2Fwww.alwaysheard.com

Any help will be highly appreciated. Many Thanks.

This is for a expressjs server.

This is my frotend calling code:

$.ajax({
        url: _url,
        data: { uname: "embed", orgin: _origin },
        // dataType: 'json',
        crossDomain: true,
        type: "GET",
        mode: 'no-cors',
        headers: {
            'mode': 'no-cors',
            // "X-TOKEN": 'xxxxx',
            // 'x-Trigger': 'CORS',
            // 'Access-Control-Allow-Headers': 'x-requested-with',
            // "Access-Control-Allow-Origin": "*",
            // 'Authorization': 'Bearer fadsfasf asdfasdf'
        },
        success: function (data) {
            console.log("data: ", data);
            $("body").html(data);
        }
    });

And in backend i'm using app.use(cors());

1 Answers1

0

Your URL is first returning a 302 redirect to the home page and then the $.ajax() system (actually the XMLHttpRequest system underneath it) is following that redirect automatically.

You can see a discussion of that topic here:

How to prevent ajax requests to follow redirects using jQuery

In newer browsers, you can use the fetch() interface instead and it contains a redirect option that allows you to tell it not to follow redirects.

FYI, it's unclear what you're really trying to accomplish here. There is no "page" to retrieve from this URL. That returns a 302 status and a few headers, no page.


In case this isn't clear to you, your long url returns this response:

HTTP/1.1 302 Found
Date: Fri, 18 Oct 2019 17:04:05 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 23
Connection: keep-alive
Server: nginx/1.14.1
Access-Control-Allow-Origin: *
Set-Cookie: _csrf=j6JfsEb_JbmSJlvHDoyhgatS; Path=/
Set-Cookie: connect.sid=s%3A8C1PVNBcm5395dwmPJ4Xbl7BNASxPH4W.WFZ6uz3NklxaObnWvZaZXI%2BoH5VdM9dnOi2VCTw3J%2BI; Path=/; Expires=Mon, 15 Oct 2029 17:04:05 GMT; HttpOnly
Surrogate-Control: no-store
Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate
Pragma: no-cache
Expires: 0
Location: /
Vary: Accept

This content was retrieved with curl -l -v yourlongurl.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • could you give an example of fetch interface with redirect option plz? – Rezoanul Alam Riad Oct 18 '19 at 16:34
  • @RezoanulAlamRiad - The redirect option is described [here](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch). It's trivial to use. What problem are you really trying to solve here? There's no content (other than headers) in the redirect response so I can't tell what problem you're really trying to solve here. – jfriend00 Oct 18 '19 at 16:39
  • it should load the contents of that link. the content should be this: https://www.alwaysheard.com/test.html – Rezoanul Alam Riad Oct 18 '19 at 16:41
  • @RezoanulAlamRiad - What exact URL are you trying to load with `$.ajax()`? Because `https://www.alwaysheard.com/embed/Medalist/brand/$2a$10$Z9ib22YZyRWWZz/Vlzju3u9eZCZM.a8oUYorNPsKGzLPKu6vM696K?uname=embed&orgin=https%3A%2F%2Fwww.alwaysheard.com` does not load or redirect to `https://www.alwaysheard.com/test.html`. It returns a 302 response with a location header of `/`. – jfriend00 Oct 18 '19 at 16:44
  • this url: https://www.alwaysheard.com/test.html loads the desired content. and it is actually calling this url: https://www.alwaysheard.com/embed/Medalist/brand/$2a$10$Z9ib22YZyRWWZz/Vlzju3u9eZCZM.a8oUYorNPsKGzLPKu6vM696K?uname=embed&orgin=https%3A%2F%2Fwww.alwaysheard.com under the hood. Now i need to load those content from a different origin/domain. Is it understable now? – Rezoanul Alam Riad Oct 18 '19 at 16:45
  • @RezoanulAlamRiad - I'm totally confused about what your question is. Please add a step by step process to your question for what you expect to happen or what you're actually trying to do. I tried to help based on my limited understanding of your question. If this isn't what you wanted, then I can't help any more unless you clarify your question. – jfriend00 Oct 18 '19 at 16:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201110/discussion-between-rezoanul-alam-riad-and-jfriend00). – Rezoanul Alam Riad Oct 18 '19 at 16:47
  • Ok, simply I need to load this url: https://www.alwaysheard.com/embed/Medalist/brand/$2a$10$Z9ib22YZyRWWZz/Vlzju3u9eZCZM.a8oUYorNPsKGzLPKu6vM696K?uname=embed&orgin=https%3A%2F%2Fwww.alwaysheard.com from a different domain. which is this: https://medalist-a805c.web.app/HeardSuggest_Medalist.html – Rezoanul Alam Riad Oct 18 '19 at 16:52
  • @RezoanulAlamRiad - All that URL returns is a set of headers. I added the actual response to the end of my question so you can see what it is. I'm still confused about what you want. – jfriend00 Oct 18 '19 at 17:05
  • i was able to sort out the problem with your answer. i got the clue. thanks a lot! – Rezoanul Alam Riad Oct 21 '19 at 18:50