The best place to learn about CORS would be Mozilla's Documentation, which I linked above, there is not too much to other then once it is properly enabled a javascirpt browser client can make post requests to it. To learn about settings up endpoints using AWS serverless functions I would recommend following Amazons Tutorials, there is a lot here, so it can take quite some time to fully understand everything.
A few things here, first the reason it is being blocked is becuase browsers these days block most post requests from the client unless the endpoint specifically enables Cross-Origin Resource Sharing (CORS). Which the hubspot endpoints do not. To get around this you'll need to set up another endpoint on a seperate server. I would recommend using AWS serverless functions, which are free for most users' small loads, and you can enable CORS on your endpoints.
Secondly do not put this code in the browser ever. You had the api key in the browser, if for some reason HubSpot did enable CORS, any person could go into the wordpress code and grab your api key, and grab all of your contact data, and then delete all of your contact for example. Never use your api key client side. Even if somehow you encrypt it, the request will still be unencrypted and they grab the api key by intercepting the request. This can also be fixed by setting up an endpoint soemwhere like AWS, where the call to this endpoint does not include the api key, but the code in AWS does have it.
Lastly, it looks like you are using a form to create leads, this can be completely avoided if you create a hubspot form which you can embed in your website. Then you do not have to mess with any code, or wory about your API key at all, and leads just get created when they fill out the form.
If you are just trying to test your API calls, then I would recomend a REST client, like Hopscotch or Postman. Neither of which have this CORS restriction. Another solution would be to test your API calls form the command line using a tool like curL, or running a Node app from the commandline, where you could still use axios.
✔️ Was I able to help answer your question? Help the community by marking it as a solution.
Thanks for your reply. I'm new to development and I just wanted to try my hand at using APIs. Would you be able to point me to any resources where I can learn more about CORS in layman terms, setting up endpoints on separate servers, using AWS serverless functions, etc? That would greatly help me on the academic front.
That being said, I also want to get my website up and running using my own form designs. Do you know what the best way to do this is? I read somewhere that I could embed a hubspot form in my html and then hide it, and use js to submit the form when the user clicks my button. Thanks for your help.
The best place to learn about CORS would be Mozilla's Documentation, which I linked above, there is not too much to other then once it is properly enabled a javascirpt browser client can make post requests to it. To learn about settings up endpoints using AWS serverless functions I would recommend following Amazons Tutorials, there is a lot here, so it can take quite some time to fully understand everything.