0

GetResponse API integration Using Fetch API method not working

The following SMS is showing in the console:

Cross-Origin Request Blocked:
The Same Origin Policy disallows reading the remote resource at
https‍://api.getresponse.com/v3/contacts.
(Reason: CORS header 'Access-Control-Allow-Origin' missing).

Code is given below:

// main.js

// POST request using fetch()
fetch("https://api.getresponse.com/v3/contacts", {
 
 // Adding method type
 method: "POST",
 
 // Adding body or contents to send
 body: JSON.stringify(
 {
 campaign : {
 campaignId: "5D8Qm"
 },
 name: "xyz",
 email: "[email protected]"
 }
 ),
 
 // Adding headers to the request
 headers: {
 
 
 "X-Auth-Token": "api-key o9q5s264jbp9dws0nsevnagqdst81esh",
 "Content-type": "application/json"
 }
})
// Converting to JSON
.then(response => response.json())
// Displaying results to console
.then(json => console.log(json));
Brian Tompsett - 汤莱恩
5,92972 gold badges63 silver badges135 bronze badges
asked Oct 4, 2021 at 0:22
4
  • 2
    Check your request and response headers. This is classic CORS issue (more: access-control-allow-origin) Commented Oct 4, 2021 at 0:28
  • Thank you for your answer. Actually, When I used the cURL, it was working properly, but when I was working with the Fetch API, it was not working . I want to know the reason for this. Commented Oct 4, 2021 at 0:36
  • 1
    "I want to know the reason for this" -> the CORS is a browser related thing. and that is why when you use cURL it didn't happen. it is a browser-specific defensive mechanism Commented Oct 4, 2021 at 6:51
  • 1
    If you use cURL, ensure you use it with: curl -s -D - https://api.getresponse.com/v3/contacts, where -s: Avoid showing progress bar, and -D: Dump headers to a file and -: forces the redirect to stdout. The headers of the HTTP protocol are important. the CORS standard happens within these headers. Commented Oct 4, 2021 at 15:29

1 Answer 1

1

It's a classic CORS issue as azbarcea said.

As for your comment about why cURL works but Fetch API not, you can refer to this answer in Stack Overflow.

answered Oct 4, 2021 at 6:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.