Obtain HTML from other sites via ajax

Asked 2 years ago, Updated 2 years ago, 98 views

I want to use ajax to get Html from other sites, but an error occurs in the cross domain.
I thought I could get it with jsonp, so I tried using the code below.

$.ajax({
    url: 'https://hoge.jp',
    dataType: 'JSONP',
    type: 'GET',
    success:function({data}){
        console.log(data);
    }
});

Uncaught SyntaxError: Unexpected token <

and browser errors.
I don't know what to do because the site doesn't even support cors.
Is there any other way to deal with this?

javascript jquery ajax

2022-09-30 16:22

2 Answers

In Ajax communication using browser and javascript, data retrieval from outside the domain is not accessible unless the site accepts it in the Access-Control-Allow-Origin header.
https://developer.mozilla.org/ja/docs/Web/HTTP/HTTP_access_control

As others have answered, scraping with a server is common, so please consider that.


2022-09-30 16:22

I don't know if I can use the server, but there is also a way to get it between php.
http://php.net/manual/ja/function.file-get-contents.php
I don't know if it will work.


2022-09-30 16:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.