2

I should preface by saying that this is in a SFMC Landing Page and I'm trying to send form data to another cloudpage to handle it.

Current code:

$('form').submit(function (e) {
  e.preventDefault();
  var json = JSON.stringify($('#subForm').serializeJSON());
  console.log(json);

  $.ajax({
    url: 'https://pub.s6.exacttarget.com/hpwryj1b4qc',
    type: 'POST',
    data: json,
    contentType: "application/json",
    success: function(json){
      console.log(json);
    },
    error: function( jqXhr, textStatus, errorThrown ){
      console.log( errorThrown );
    }
  });
});

Returns:

<script>(function(a,m,i,g,o,s){o=a.createElement(g);s=a.getElementsByTagName(i)[0];o.src=m.origin+m.pathname+"/_t?eventType=CLOUDPAGESVISIT";o.width=0;o.height=0;o.style.display="none";s.appendChild(o);})(document,window.location,"body","img");</script>
<script src="https:&#x2F;&#x2F;*orgid*.collect.igodigital.com&#x2F;collect.js"></script>
<script>
    if (_etmc && typeof _etmc.push === 'function') {
        _etmc.push(['setOrgId', '*orgid*']);
        _etmc.push(['trackPageView']);
    }
</script>

Before the ajax request runs, the console.log of the "json" variable returns:

{"Email":"*myemail*","SoB_001":"false","SoB_002":"false","SoA_001":"false","SoA_002":"false","SoA_003":"false"}

Setting the dataType to json breaks the post and I get the "syntaxError: Unexpected token < in JSON at position 4" error. What am I missing to send JUST the json object?

  • It's probably returning an html formatted error - perhaps a 401 or 500. Try using postman to submit the request (it's a chrome extension) – Caspar Harmer Jun 19 '18 at 4:04
  • Where do set the dataType to json? I can see contentType. It's probably an error in ajax. can you show more of the error? – Caspar Harmer Jun 19 '18 at 4:15
  • Here is the full error with the dataType setting: SyntaxError: Unexpected token < in JSON at position 8 at parse (<anonymous>) at Ut (jquery.min.js:2) at k (jquery.min.js:2) at XMLHttpRequest.<anonymous> (jquery.min.js:2) – Matt Goldstein Jun 19 '18 at 7:08
  • Is this in lightning? if so you can't do callouts from here... you have to do it in the Apex controller. – Caspar Harmer Jun 19 '18 at 7:22
  • No, this is in Cloudpages Classic Editor. – Matt Goldstein Jun 19 '18 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.