SOLVED

Munchkin code problem

Go to solution
Harish_Gupta6
Level 8

Munchkin code problem

Hi, I have simple 1 html page. I have added munchkin code in that page. I am trying to create lead in Marketo from html page but its not working. The SHA1 key i have generated using http://www.sha1-online.com/
key = xxx
value = "ba4de953201fd7fb30def96cbcd182731584a356"

I have hosted this page on tomcat. I am facing error: "
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://876-swj-450.mktoresp.com/webevents/visitWebPage?_mchNc=1429178936958&_mchCn=&_mchId=876-SWJ-4.... (Reason: CORS request failed)."

I have already enabled cross domain in tomcat web file.

Below is the code for page

<html>
<head>
  <title>Page 1</title>
  <script type="text/javascript">
  (function() {
    var didInit = false;
    function initMunchkin() {
      if(didInit === false) {
        didInit = true;
        Munchkin.init('xxxxxx');
        console.log("init");
      }
    }
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'https://munchkin.marketo.net/munchkin.js';
    s.onreadystatechange = function() {
      if (this.readyState == 'complete' || this.readyState == 'loaded') {
        initMunchkin();
      }
    };
    s.onload = initMunchkin;
    document.getElementsByTagName('head')[0].appendChild(s);
  })();
  </script>
<script type="text/javascript">
       function MarketoUpdate() {
         alert("inside fun");
         Munchkin.munchkinFunction('clickLink', {
    href: '/MyFlashMovie/Story1'}
        );
        alert("after fun 1");
       Munchkin.munchkinFunction('associateLead',{
         Email: 'megha_64@yahoo.com',
         FirstName: 'kohli',
         LastName: 'megha' },
        'ba4de953201fd7fb30def96cbcd182731584a356');
        alert("ret fun");
  }
</script>
</head>
<body>
  <form name="form1" onsubmit="MarketoUpdate()">
  <label for="fname">Fisrt Name</label>
  <input type="text" name="fname"/>
  <label for="lname">Last Name</label>
  <input type="text" name="lname"/>
  <br/>
  <label for="email">Email</label>
  <input type="email" name="email"/>
  <br/>
  <label for="company">Company</label>
  <input type="text" name="company"/>
  <br/>
  <input type="submit" value="Submit"/>
  </form>
  <a href="page2.html">Link to page 2</a>
  <a href="page3.html">Link to page 3</a>
 
</body>
</html>

Please suggest what's wrong in this code?

Harish Gupta
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Munchkin code problem

When debugging, make sure you reduce and simplify your code to the smallest part that is triggering an error.

In this case the error you posted has nothing to do with the associateLead() call.  The hit to ... visitWebPage occurs when you run Munchkin.init(), and it's that (attempted) hit that's causing the error you posted.

Anyway, I suspect you're running off http://localhost and testing in Chrome.  Chrome has a bug where it flags CORS errors in this case, which can be confusing.  Create an entry in your HOSTS file that points, for example, tomcat.example.com to 127.0.0.1.  Then test using http://tomcat.example.com.  This works around the Chrome bug.

Also, you said you "enabled cross-domain" in Tomcat.  The CORS settings for Munchkin aren't made on your server, they are made on Marketo's servers (where they are also in place).  

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Munchkin code problem

When debugging, make sure you reduce and simplify your code to the smallest part that is triggering an error.

In this case the error you posted has nothing to do with the associateLead() call.  The hit to ... visitWebPage occurs when you run Munchkin.init(), and it's that (attempted) hit that's causing the error you posted.

Anyway, I suspect you're running off http://localhost and testing in Chrome.  Chrome has a bug where it flags CORS errors in this case, which can be confusing.  Create an entry in your HOSTS file that points, for example, tomcat.example.com to 127.0.0.1.  Then test using http://tomcat.example.com.  This works around the Chrome bug.

Also, you said you "enabled cross-domain" in Tomcat.  The CORS settings for Munchkin aren't made on your server, they are made on Marketo's servers (where they are also in place).  
Harish_Gupta6
Level 8

Re: Munchkin code problem

Thanks Sanford, Actually i was using wrong API key. thanks for your help. 🙂
Harish Gupta
SanfordWhiteman
Level 10 - Community Moderator

Re: Munchkin code problem

The API key is not used for the Munchkin.init() visitWebPage call, so that cannot have been the cause of the CORS error (the server does not receive the API key at that step, so it can't validate it). Glad it is working now in any case.