Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cant seem to resolve this issue, i am using Chrome. The idea i want to download CVS file using Ajax call request.

What I have tried:

$(document).ready(function() {
    $("#download").click(function() {
      $.ajax({
        url:'https://www.thingspeak.com/channels/899906/feeds.json?api_key=F**&results=3',
        type:'GET',
        dataType:'json',
        success:function(response){
          window.location = response;
        }
      });

    });

  });


I am trying to follow this link, the idea is for me to download from my button date range for the field. On VS code i can able to do this without no problem including using the URL itself, MY CVS file does come right. Read data from all fields in a channel with HTTP GET - MATLAB- MathWorks Benelux[^]
Posted
Updated 10-Nov-19 23:51pm

The site you are accessing has to allow pages outside of its domain from accessing it via ajax. It does this by sending headers back that say what you are doing is allowed. If the target site is not doing this then you can't script access to it. This is what CORS is for, to explicitly stop people doing what you are doing.

To preempt your next question, if there was a way to get around this it wouldn't be a very good security feature. You'll need to see if there is an appropriate API the target site supplies that you are able to use.
 
Share this answer
 
Comments
gcogco10 11-Nov-19 5:13am    
From this link, there are not much examples https://community.thingspeak.com/tutorials/javascript/parse-thingspeak-last-entry-feed-with-jquery/
Hi Team

I have managed to work around to this problem though, here is my solution i was looking for all along.

$(document).ready(function() {
         $("#download").click(function() {
         $.ajax({
           url:'https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-08%2019:10:08&end=2019-11-11%2019:11:11',
           crossDomain:true,
           type:'GET',
           data:{
             format:'csv',
           },
           success:function(response){
             window.location.href ='https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-08%2019:10:08&end=2019-11-11%2019:11:11' ;
           },
           error:function() {
             $().text("There was an error processing your request.Please try again");
             }
         });
         });

       });


The issue were 2 things, i remove URL https://www-api.thingspeakcom/......???
Second one, was my window.responce location to receive the response back as any file format on my data. Thanks all
 
Share this answer
 
Comments
Richard Deeming 12-Nov-19 11:29am    
Why are you requesting a URL via AJAX, waiting for it to return, then throwing away the response and redirecting the user to exactly the same URL you've just requested?

Simply redirect the user to that URL in the first place.
$("#download").click(function() {
    window.location.href = 'https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-08%2019:10:08&end=2019-11-11%2019:11:11';
});
gcogco10 13-Nov-19 8:08am    
Thanks Richard Deeming, i have tested second logic you gave, the logic seem to work fine and sending the correct date format as per requested from ajax call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900