Hubspot form API [RESOLVED]

I’m trying to use api form (https://developers.hubspot.com/docs/methods/forms/submit_form_v3) on my site , but I get this error, HTTP ERROR 405
Problem accessing / submissions / v3 / integration / submit / XXX / xxxxxx. Reason: Method Not Allowed

 var request = $.ajax({ 
 url: "https://api.hsforms.com/submissions/v3/integration/submit/MyPportalid/FormId", 
 method: "POST",
 data: { 
 "fields": [
 {
 "name": "firstname",
 "value": firstname
 },
 {
 "name": "lastname",
 "value": lastname
 },
 {
 "name": "email",
 "value": email
 },
 {
 "name": "phone",
 "value": phone
 },
 {
 "name": "company",
 "value": company
 },
 {
 "name": "dataMessage",
 "value": dataMessage
 }
 ],
 "context": {
 "hutk": ":"+hubspotutk, 
 "pageUri": pageUrl,
 "pageName": pageName
 } ,
 "legalConsentOptions": {
 "consent": { 
 "consentToProcess": true,
 "text": "I agree to allow Example Company to store and process my personal data.",
 "communications": [
 {
 "value": true,
 "subscriptionTypeId": 999,
 "text": "I agree to receive marketing communications from Example Company."
 }
 ]
 }
 }
 },
 dataType: "json"
 });

 request.done(function(msg) {
 console.log( "success "+msg );
 })
 .fail(function(msg) {
 console.log( "error "+msg );
 })
 .always(function(msg) {
 console.log( "complete "+msg );
 });

The method is in POST, why i get that message ?

Hi @francobolla,

I hope all is well with you :smiley:

When looking to submit a form using ajax, we can look at the endpoint here: https://developers.hubspot.com/docs/methods/forms/submit_form_v3

  • It looks like you’re missing te contentType and setting the datatype as json
  • The following codes works for me:
 $.ajax({
 url: "https://api.hsforms.com/submissions/v3/integration/submit/4753304/0db94b1d-bdc0-4da9-a454-97d1ff454413",
 type: "POST",
 contentType: "application/json",
 dataType: "json",
 headers: {
 "accept": "application/json",
 "Access-Control-Allow-Origin":"*"
 }
 data: JSON.stringify({

 "fields": [
 {
 "name": "email",
 "value": getEmail
 },
 {
 "name": "firstname",
 "value": getName
 }
 ],
 }),

 success: function(result) {
 console.log(result);
 },
 error: function(xhr,status,error) {
 console.log(xhr);
 console.log(status);
 console.log(error);
 }
 });

Hope this helps to clarify :slightly_smiling_face:

@WendyGoh ooh thank you very much, it works great.