1

Possible Duplicate:
How do I send a cross-domain POST request via JavaScript?

Created a HTML page with form field, while hitting form, the form details posted in other domain Market site.

<form  method="Post"  action="http://app-o.marketo.com/index.php/leadCapture/save"  name="mktForm_6" id="mktForm_6">

    <label>First Name:</label><span class='mktInput'><input  name="FirstName" id="FirstName" type='text' value=""  maxlength='255' tabIndex='1' />

    <label>Email Address:</label><input  name="Email" id="Email" type='text' value=""  maxlength='255' tabIndex='2'`enter code here` />

    <label>Subscription</label><input class='mktFormHidden' name="Subscription_Expiration__c" id="Subscription_Expiration__c" type='hidden' value="Newsletter Subscription" />
    <input id='mktFrmSubmit' type='submit' style="width: auto; overflow: visible; padding-left: .25em; padding-right: .25em;" value='Submit'  />

    <input type="hidden" name="lpId" value="1030" />

    <input type="hidden" name="subId" value="123" />

    <input type="hidden" name="searchstr" value="" />

    <input type="hidden" name="formid" value="6" />

    <input type="hidden" name="_mkt_disp" value="return" />

    <input type="hidden" name="_mkt_trk" value="id:668-TIV-019&token:_mch-syncfusion.com-1350287989102-87089" />

</form>

But now I want to post the form details via AJAX, because normal form post refresh the page.

Can anyone suggest how to post the details in $.post() method of AJAX with clear details?

phuzi
  • 12,078
  • 3
  • 26
  • 50
Anandh
  • 167
  • 5
  • 15

1 Answers1

2

Try This

var data = { Firstname: $("#firstnametxt")[0].value, Email: $("#txtemail")[0].value};}
var jsondata = JSON.stringify(data);
$.ajax({
    type: 'POST',
    url: '@Url.Action("SaveCustomer","Customer")',
    data: jsondata,
    dataType: "html",
    cache: false,
    contentType: "application/json; charset=utf-8",
    success: function (_results, status, message) {
       $("#Targetdiv").html(_results);
    },
    error: function (_results, status, message) {

    }
});

Have a View model similar to this.

public class Customer
{
    public String FirstName{get;set;}
    public String Email{get;set;}

}

And receive the data in controller like this.

[HttpPost]
public ActionResult SaveCustomer(Customer _Customer)
{
    //Receive the posted json data as your view model object(_Customer) here
    //Asp.net translate the same for you.
}

If You are using IE use json2.js which is available Here

Milad Rashidi
  • 1,296
  • 4
  • 22
  • 40
amesh
  • 1,311
  • 3
  • 21
  • 51