Hi Anders,
I'd highly discourage you from executing rest calls in Javascript on the page. This exposes your access token publicly, which can allow your session to be highjacked. It's far safer to execute the REST call on the server and then put the needed results into the page which loads for the user. Depending on what server architecture you use it will vary how you can execute this on the server, but there are some libraries available for Ruby(https://github.com/jalemieux/mkto_rest) and PHP(https://github.com/dchesterton/marketo-rest-api) which you can look into. Once you've got the call working on the serverside, passing the results into the form is fairly simple. Here's an example snippet:
<script>
//take your results from the server and set them as a variable on the page
var mktoLead = {
"requestId":"e42b#14272d07d78",
"success":true,
"result":[
{
"id":60,
"email":"kjashaedd@klooblept.com",
"firstName":"Kataldar",
"updatedAt":"2013-11-21T11:47:30-08:00"
}
]
};
MktoForms2.whenReady( function(form) {
var mktoLeadFields = mktoLead.result[0];
//map your results from REST call to the corresponding field name on the form
var prefillFields = {
"Email" : mktoLeadFields.email,
"FirstName" : mktoLeadFields.firstName
};
form.vals(prefillFields);
}
);
</script>