如何获取数据使用来自ajax json post的Classic ASP

时间:2014-01-09 03:48:23

标签: jquery ajax json asp-classic

我正在尝试通过ajax json学习将数据发送到经典asp的方法 这个asp页面已被json成功调用,但问题是数据无法到达后端,因此数据库无法根据修改后的类型进行修改

正面

    <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
%>

1 个答案:

答案 0 :(得分:5)

我认为问题是请求是以JSON格式发送的,所以当你尝试获取“sort”的值时,它在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>
相关问题