0

I use AJAX to call a PHP file to load a JSON from an URL. This works without problem

PHP Code:

$ch = curl_init();                                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     
curl_setopt($ch, CURLOPT_URL, 'https://query1.finance.yahoo.com/v1/finance/search?q=tesl&quotesCount=10&newsCount=0');       
$result = curl_exec($ch);                           
curl_close($ch);                                    

$obj = json_decode($result);
print_r($obj);

Now i want to change it, so its loaded with jQuery from the clients Browser and not from the Webserver. So i tried this jQuery code:

jQuery.ajax({
            dataType: 'jsonp',
            url: 'https://query1.finance.yahoo.com/v1/finance/search?q=tesl&quotesCount=10&newsCount=0',
            type: 'GET',
            success: function(data){
                console.log( data );
            }
        });

I tried some variation but i always get this Error

Cross-Origin Read Blocking (CORB) blocked cross-origin response
 https://query1.finance.yahoo.com/v1/finance/search?q=asdf&quotesCount=10&newsCount=0&callback=jQuery351065066686635359_1614093535685&_=1614093535686 with MIME type application/json. 
See https://www.chromestatus.com/feature/5629709824032768 for more details.

What am i doing wrong? Is there an other way to load the JSON with JS so its not going through the Webserver?

Thanks

nathan
  • 1
  • There's two parts to the problem. You're receiving a CORB error because you've told jQuery to expect a JSONP response, but the format is actually JSON. Given that when you correct that issue you instead receive a CORS error, I assume you've done this due to some bad advice to work around the CORS restrictions on cross-domain AJAX requests. Unfortunately simply changing the expected response type will not work in this case. You cannot make a cross-domain AJAX request unless the receiving domain expressly permits it with CORS headers. In this case they have not. – Rory McCrossan Feb 23 '21 at 15:40
  • To fix the issue, make the cross-domain call from your server side, not JS. See the duplicate I marked for more information. – Rory McCrossan Feb 23 '21 at 15:41

0 Answers0