jquery script is:
<script>
$(document).ready(function () {
$("#LeadForm").submit(function (event) {
$.ajax({
type: "POST",
url: "http://localhost:9135/api/CRMLead",
data: $("#LeadForm").serialize(),
dataType: 'json',
success: function (response) {
alert(response);
$('#LeadForm').each(function () {
this.reset(); //Reset each field
});
},
error: function (request, textStatus, errorThrown) {
alert(request.responseText + " " +
textStatus + " " + errorThrown);
}
});
event.preventDefault();
});
});
</script>
controller is:
public class CRMLeadController : ApiController
{
private OrganizationService _orgService;
[EnableCors(origins: "*", headers: "*", methods: "post")]
public string Post([FromBody] FormDataCollection formValues)
{
string domain = HttpContext.Current.Request.Headers["Origin"].ToLower();
string host = HttpContext.Current.Request.Url.Host.ToLower();
//if (!domain.Contains("yourdomain.com") && !domain.Contains(host))
// return "fail!";
CrmConnection connection = CrmConnection.Parse(
ConfigurationManager.ConnectionStrings["CRMOL"].ConnectionString);
using (_orgService = new OrganizationService(connection))
{
Entity lead = new Entity("lead");
lead["firstname"] = formValues.Get("FirstName");
lead["lastname"] = formValues.Get("LastName");
_orgService.Create(lead);
}
return "success";
}
}
}
CRM Connection is:
<add name="CRMOL" connectionString="AuthType=Office365; Username=xxxx@xxxxxx.onmicrosoft.com; Password=xxxx; Url=https://xxxxx.crm.dynamics.com "/>
Thanks in advance