2014-01-09 91 views
0

我想學習一種方法通過ajax json發送數據到經典asp asp頁面已被json成功調用,但問題是數據無法返回側,因此數據庫無法根據修訂後的排序來修改如何獲取數據使用經典ASP從ajax json post

前側

<script> 
    $(document).ready(function(){ 
    $("#sortable").sortable({ 
     update: function(event, ui) { 
     alert(ui.item.index()); 
     alert(ui.item.attr("id")); 
     var data={ 
      'sort':ui.item.index() 
      } 
     $.ajax({ 
       url: 'work.asp?action=update, 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       data: JSON.stringify(data), 
       dataType: "json", 
       error: function (xhr, status, error) { 
        var err = eval("(" + xhr.responseText + ")"); 
        alert(err.Message); 
       }, 
       success: function(){ 
        alert("success"); 
       } 
     }); 
    } 
    }); 
    }); 
    </script> 

背面我用傳統的ASP 但我無法獲得數據 數據我怎麼能做到嗎?

<% 
Set conn = Server.CreateObject("ADODB.Connection") 
DBPath = Server.MapPath("wawaaddatatable.mdb") 
conn.Open "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath 


if request.QueryString("action")="update" then 

Response.ContentType = "application/json" 

Set rs = Server.CreateObject("ADODB.Recordset") 
rs.open "Select * From images ",conn,1,3 

rs.addnew 
rs("sort")=request.form("sort") 
rs("upload_date")=date() 
rs("filename")="abc.jpg" 
rs.update 
rs.close 

end if 
%> 

回答

5

我覺得現在的問題是,要求以JSON格式發送的,所以當你嘗試並獲得的「排序」值不Request.Form集合中存在。雖然你可以解析JSON並獲得我建議的值,但是將請求更改爲普通表單發佈會更容易。

我修改了下面的腳本。請參閱我在其中添加的評論。通過這些更改,Request.Form(「sort」)將返回排序順序值的逗號分隔字符串,因此您現有的後端應該仍然有效。

<script> 
    $(document).ready(function(){ 
    $("#sortable").sortable({ 
     update: function(event, ui) { 
     alert(ui.item.index()); 
     alert(ui.item.attr("id")); 
     //following not needed now 
     //var data={ 
     // 'sort':ui.item.index() 
     // } 
     $.ajax({ 
       url: "work.asp?action=update", // update 
       type: "POST", 
       //Removed contentType so default will be used which is normal form post data 
       //contentType: "application/json; charset=utf-8", 
       //Changed data to use this method which sends order as comma separated string 
       data: { sort: $('#sortable').sortable('serialize') }, 
       dataType: "json", 
       error: function (xhr, status, error) { 
        var err = eval("(" + xhr.responseText + ")"); 
        alert(err.Message); 
       }, 
       success: function(){ 
        alert("success"); 
       } 
     }); 
    } 
    }); 
    }); 
    </script> 
+0

感謝您的幫助! –