My domain is blocked by CORS policy: No 'Access-Co​ntrol-Allo​w-Origin' header is present on the requested resource.

조회 수: 24 (최근 30일)
Since this week I cannot add a talkback command using Javascript/Ajax. I get the following error in my web browser (Chrome)
Access to XMLHttpRequest at 'https://api.thingspeak.com/talkbacks/32490/commands.json?apikey=xxxxxxxxxxxxxxx&command_string=OFF&position=1'
from origin 'https://desmon.fr' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
However the same code was working perfectly 2 weeks ago. It seems that my website is blocked on ThingSpeak.
When I try to submit the same command using CUIRL it works (from my PC).
curl -X POST https://api.thingspeak.com/talkbacks/32490/commands.json?api_key=xxxxxxxxxxxxxxxxxx -d "command_string=ON&position=1"
Here is the Javascript code I use:
$.ajax({
type: 'POST',
url: 'https://api.thingspeak.com/talkbacks/32490/commands.json?apikey=xxxxxxxxxxxxxxxxxxxxxxx&command_string='+newState+'&position=1',
crossDomain : true,
success: function(data) {
  댓글 수: 2
Vinod
Vinod 2019년 10월 21일
편집: Vinod 2019년 10월 21일
Can you tell us more about the use case -- why is the web browser posting commands to talkbacks?
Talkbacks are primarily intended for use by devices. The CORS policy we have prevents this use from a web browser which does some additional validation on the client side when making the requests. We can look into putting in headers to prevent the browser from doing these checks depending on the use case.
jM Desmon
jM Desmon 2019년 10월 21일
I use talkback commands to remotely switch on/off my boiler. I use a web page to send commands. How can I work around this issue?

댓글을 달려면 로그인하십시오.

채택된 답변

Vinod
Vinod 2019년 10월 23일
On further investigation, it appears that there is a difference between your AJAX code and the CURL command:
AJAX request is:
https://api.thingspeak.com/talkbacks/32490/commands.json?apikey=xxxxxxxxxxxxxxx&command_string=OFF&position=1
CURL request is
https://api.thingspeak.com/talkbacks/32490/commands.json?api_key=xxxxxxxxxxxxxxxxxx -d "command_string=ON&position=1
Note the difference "apikey" v/s "api_key".
Can you please modify your AJAX request to use api_key and try again?
  댓글 수: 1
jM Desmon
jM Desmon 2019년 10월 28일
You are totally right. I didn't note this difference. I just modified the AJAX request with api_key and it works. I do not understand why but it works. Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Write Data to Channel에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by