jQuery ajax request being block because Cross-Origin

后端 未结 4 1754
慢半拍i
慢半拍i 2020-11-30 08:42

How to get content from remote url via ajax?

jQuery ajax request being block because Cross-Origin

Console Log

Cross-O

相关标签:
4条回答
  • 2020-11-30 09:11

    I solved this by changing the file path in the browser:

    • Instead of: c/XAMPP/htdocs/myfile.html
    • I wrote: localhost/myfile.html
    0 讨论(0)
  • 2020-11-30 09:20

    There is nothing you can do on your end (client side). You can not enable crossDomain calls yourself, the source (dailymotion.com) needs to have COORS enabled for this to work.

    The only thing you can really do is to create a server side proxy script which does this for you. Are you using any server side scripts in your project? PHP, Python, ASP.NET etc? If so, you could create a server side "proxy" script which makes the HTTP call to dailymotion and returns the response. Then you call that script from your Javascript code, since that server side script is on the same domain as your script code, COORS will not be a problem.

    0 讨论(0)
  • 2020-11-30 09:35

    Try to use JSONP in your Ajax call. It will bypass the Same Origin Policy.

    http://learn.jquery.com/ajax/working-with-jsonp/

    Try example

    $.ajax({
        url: "https://api.dailymotion.com/video/x28j5hv?fields=title",
    
        dataType: "jsonp",
        success: function( response ) {
            console.log( response ); // server response
        }
    
    });
    
    0 讨论(0)
  • 2020-11-30 09:35

    Try with cURL request for cross-domain.

    If you are working through third party APIs or getting data through CROSS-DOMAIN, it is always recommended to use cURL script (server side) which is more secure.

    I always prefer cURL script.

    0 讨论(0)
提交回复
热议问题