Can't send form data to 3rd-party API

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?

Thanks for any input.

Steve

Hi @SRalston05

Please insert the following key in your request JSON:
CORS: false

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!

Some research makes me think if it was going to be anything it would be crossDomain:true.

Regardless, the call still fails, I’m afraid.

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.

Hi @SRalston05,

Do you have access to Operation Hub Webhooks? The kind you can use in a workflow? Or do you mean private app Webhooks.

If it is the first, it is quite easy to set that up. And you can completely customise the request body.

If it is the second, you need to spin up your own endpoint and catch the incoming request with something like NodeJS or PHP.

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.

Did you set the re-enrollment correctly for the workflow? Because you should be able to trigger the workflow multiple times.

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.

That is the HubSpot tracking script magic.

A cookie is set to identify the user, and because of it, it’s not creating new contacts for each form submission.

There is a setting in the form editor to disable that behaviour, but I would advise against it to use in production.

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!

Awesome! Happy I could help!