2012-10-15 30 views
1

可能重複:
How do I send a cross-domain POST request via JavaScript?AJAX:帖子不網頁表單細節刷新

創建以表單字段一個HTML網頁,同時擊中表格,表格信息發佈在其他領域市場現場。

<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的形式的詳情,因爲普通形式後刷新頁面。

任何人都可以建議如何發佈詳細信息在$.post()方法的AJAX與明確的細節?

+0

我請你閱讀jQuery的阿賈克斯($阿賈克斯) –

+1

http://stackoverflow.com/questions/298745/我怎麼做,我發送一個跨域後請求通過JavaScript – sircapsalot

+0

@ChandraSekharWalajapet,以及如何'$ .ajax'幫助POST表單數據到另一個域? –

回答

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) { 

    } 
}); 

有一個類似的視圖模型。

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

+0

他想將表單發佈到另一個網站(不同的域)。 '$ .ajax'在這種情況下不起作用。 –