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?
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.
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.
© 2024 OneMinuteCode. All rights reserved.