I've been working on this for months now and have talked with my host, the company etc and I can make my api call work on their developer page which has example code to test their API but for the life of me I cannot get it to work on my Wordpress site and it is very much needed.
My initial code is as follows:
const options = {
method: 'POST',
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
Authorization: 'Basic XXXXXXXREDACTEDXXXXX'
},
body: JSON.stringify({firstName: 'John', lastName: 'Doe', phoneNumber: '5444455555'})
};
fetch('https://a.eztexting.com/v1/contacts', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
The first error I receive is: Access to fetch at 'https://a.eztexting.com/v1/contacts' from origin 'https://businessofimagination.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
after trying many different headers all over Wordpress and my theme I finally included: "mode: 'no-cors'," into my code
updated code is as follows:
const options = {
method: 'POST',
mode: 'no-cors',
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
Authorization: 'Basic XXXXXXREDACTEDXXXXXX'
},
body: JSON.stringify({firstName: 'John', lastName: 'Doe', phoneNumber: '5444455555'})
};
fetch('https://a.eztexting.com/v1/contacts', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Now the errors I receive are: POST https://a.eztexting.com/v1/contacts 403 (403 forbidden) and SyntaxError: Unexpected end of input
I don't understand why it refuses to work, or what in Wordpress is preventing this. Please any help would be incredible. This has been an incredibly frustrating journey for something that in theory should work fairly seamlessly.