可能重复:
How do I send a cross-domain POST request via JavaScript?
创建一个带有表单字段的HTML页面,同时点击表单,在其他域名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>
但是现在我想通过AJAX发布表单详细信息,因为普通表单会刷新页面。
有人可以建议如何使用明确的详细信息在AJAX的$.post()
方法中发布详细信息吗?
答案 0 :(得分:2)
试试这个
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) {
}
});
拥有与此类似的View模型。
public class Customer
{
public String FirstName{get;set;}
public String Email{get;set;}
}
并在控制器中接收数据。
[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.
}
如果您使用IE,请使用可用的json2.js Here