Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    No Access-Control-Allow-Origin - Sometimes!

    I am using a client side loop to pull data from an API (specifically the BTC price from Bitstamp). This loop runs every 5 second. Around nine times out of ten I get a perfect response and its happy days however approximately one in ten times I will get:
    Code:
    Failed to load https://www.bitstamp.net/api/v2/ticker/btcusd: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
    There seems to be no rhyme or reason to when it occurs. Itís not every tenth, it can be the first call, slowing the requests down does not help, and no other requests present this issue.
    I feel I understand the error and what it is saying, but I donít understand how itís an intermittent error, it should either occur or not occur.

    Any tips or guidance for this noob would be appreciated! Maybe even a beer

    If it helps, the code is as bellow:
    Code:
    function retrieveBitstampLatestPrice() {
    
    	console.log('retrieveBitstampLatestPrice');
    
    	var xhr_method = 'GET';
    	var xhr_url = 'https://www.bitstamp.net/api/v2/ticker/btcusd';
    	var xhr_send = null;
    
    	var xhr = new XMLHttpRequest();
    	xhr.open(xhr_method, xhr_url, true);
    	xhr.send(xhr_send);
    
    	xhr.onreadystatechange = function () {
    		if (xhr.readyState == 4) {
    			if (xhr.status == 200) {
    				var response = JSON.parse(xhr.response);
    				//console.log(response);
    				bitstampLatestPrice = response.last;
    			} else {
    				console.log('Something went wrong! (' + xhr_url + ')');
    				console.log(xhr);
    			}
    		}
    	}
    }

  2. #2
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Additional information: This is being executed locally (100% client side, no server at all)

  3. #3
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

  4. #4
    Regular Coder
    Join Date
    Sep 2010
    Location
    U S of A
    Posts
    291
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Quote Originally Posted by WudsyWudsyWudsy View Post
    Additional information: This is being executed locally (100% client side, no server at all)
    Really ?
    what about this server ...

    https://www.bitstamp.net/api/v2/ticker/btcusd

    Code:
    <!doctype html>
    
    <script>var a=0;
    retrieveBitstampLatestPrice();
    function retrieveBitstampLatestPrice() {
    
    	
    	var xhr_method = 'GET';
    	var xhr_url = 'https://www.bitstamp.net/api/v2/ticker/btcusd';
    	var xhr_send = null;
    
    	var xhr = new XMLHttpRequest();
    	xhr.open(xhr_method, xhr_url, true);
    	xhr.send(xhr_send);
    
    	xhr.onreadystatechange = function () {
    		if (xhr.readyState == 4) {
    			if (xhr.status == 200) {
    				var response = JSON.parse(xhr.response);
    				//console.log(response);
    				bitstampLatestPrice = response.last;
    				console.log(a + " -> " + bitstampLatestPrice );
    				a++;if(a<100)setTimeout(retrieveBitstampLatestPrice,5000);
    			} else {
    				console.log('Somethingt wrong! (' + xhr_url + ')');
    				console.log(xhr);
    			}
    		}
    	}
    
    }
    
    
    </script>
    Last edited by Shaka Zorba; Nov 23rd, 2017 at 12:03 AM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •