1. 程式人生 > >前臺通過js 寫個ajax請求把資料傳給後臺,然後後臺接收到這個資料, 再儲存到資料庫。。

前臺通過js 寫個ajax請求把資料傳給後臺,然後後臺接收到這個資料, 再儲存到資料庫。。

 //前端js操作:
function testAjax(){
var url="/testAjaxUrlJson/";//後臺接收處理url
var txtContent= "textprm";//傳輸內容;
 var objData = [
        { name: 'txtContent', value: txtContent}
    ];//post方式傳送的資料
    $.ajax({
        url: url
        , data: objData
        , dataType: 'json'
        , type: 'POST'
        , success: function (data) {
            if (data.Result == 'success') {
             alert("操作成功");//請求傳輸成功回撥函式
            }
            else 
            {
              alert("操作失敗");//請求傳輸失敗回撥函式
             }
        }
        , error: function (msg) {
         alert(msg); //錯誤資訊
    
        }
    });
}


//後臺接收 MVC下
public JsonResult testAjax(Formcollection form)
{
    Hashtable ht = new Hashtable();
   string content=form["txtContent"] as string;
   //資料庫操作
   //儲存資料庫中【資料操作,自己填充】
   //返回json
   bool result=false;
   if(資料庫操作成功【result==true】)
        ht["Result"]="success";
else 
   ht["Result"]="failed";
return Json(ht);
}