I’m a web developer, but not a HubSpot developer. I’m working on javascript to send a HubSpot form’s submitted data to a 3rd-party API via an ajax call. I’m providing the API key in the header of the request and sending a valid JSON object in the body. I always get the CORS “access-control-allow-origin” error when I do that. Using the exact same structure in Postman works fine. And I’ve successfully done this same thing, sending to the same API, for other customers where I made the request from PHP.
Is there any way to get past this problem using jquery? If not, any suggestions for other ways I might go about this?
Hi @SRalston05 ,
When you are using Postman or PHP, you are making a serverside API call, so CORS is not an issue there.
However, when making an API call with JavaScript directly from the website (probably using the HubSpot form events), CORS is something that has to be managed.
If you have access to the code of the 3rd-party API, you want to make sure that you update the Access-Control-Allow-Origin header, so that it includes the domain you are sending your API call from.
So let’s say I’m writing my JavaScript for a domain called example.com, on my API, I need to update the headers to include something like:
```
res.append(‘Access-Control-Allow-Origin’, ‘https://example.com’)
```
That way, my incoming API call will be accepted.
If you do not have access to the code of the 3rd party API. You will probably have to send your data to a server first and send the data to the 3rd party API from there.
That’s a good explanation, thank you very much for that. More research has led me to think a webhook might work in this situation. Do you know if I can create a webhook that fires when a specific form is submitted? This form creates a contact record. I want to send the data from certain fields in the form to the remote api. I do have access to webhooks. I made a dead simple one that currently just sends a test contact’s info to a test endpoint I use. I’m just not familiar with this hubspot territory.
The account I’m working in has the Marketing Hub Pro and CMS Hub Pro products. I have been able to make a simple workflow. Step 1 is a trigger based on someone submitting a specific form. Step 2 is sending a webhook. This worked once when I submitted the form but it hasn’t worked since.
Another good clue, thank you. I allowed re-enrollment and I can now trigger the workflow repeatedly, but it seems like maybe it’s re-enrolling the same user instead of creating new ones. I enter a user new name and email address for each test I do, but the notification emails I get have a subject of “Contact reconversion by submitting …”. And when I look at my contacts, all the previous contacts I have made via my form are gone, and the most recent contact has all email addresses of all the tests I’ve sent through my form.
Ah. I do have the ‘always create a record’ feature turned on right now, and testing in an incognito browser repeatedly does create new records.
Your suggestions and input have been invaluable. Thank you very much!