在Asp.net中使用jquery将数据插入SQL Server时出错

Error when insert data to SQL Server using jquery in Asp.net

我的代码:

Test.aspx文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    Untitled Page
    <script type="text/javascript" language="javascript" src="Scripts/jquery.js">
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btn_save').click(function () {
                $.ajax({
                    type:"POST",
                    url:"JqueryFunction.aspx/insert_remark",
                    data:"{remark:'" + $("#remark").val() +"'}",
                    contentType:"application/json; charset=utf-8",
                    dataType:"json",
                    success: function () {
                        alert("success");
                    },
                    error: function () {
                        alert("Can not insert");
                    }
                });
            });    
        });    
   
</head>
<body>
    <form id="form2" runat="server">
        Remark
        </asp:TextBox>
        <button id='btn_save' type="button">Save</button>                        
    </form>
</body>
</html>

*在JqueryFunction.aspx代码文件:

1
2
3
4
5
6
7
8
9
10
11
12
//Insert remark
    [System.Web.Services.WebMethod]
    public static void insert_remark(string remark)
    {

        string str_insert ="INSERT INTO remark VALUES(@remark)";

            SqlParameter[] parameter = {
                new SqlParameter("@remark", remark),
            };
            int abc = DBclass.TruyVan_XuLy_VoiThamSo(str_insert, parameter);
    }

如果我的话:"文本框中输入需要插入到数据库I CAN"作品的好代码。但当我输入:"I CAN’T插入到数据库",我得到这个错误:

1
{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (16): {remark:\u0027I can\u0027t insert to

database\u0027}","StackTrace":" at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32
depth)

at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32
depth)

at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String
input, Int32 depthLimit, JavaScriptSerializer serializer)

at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer
serializer, String input, Type type, Int32 depthLimit)

at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String
input)

at
System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext
context, JavaScriptSerializer serializer)

at
System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData
methodData, HttpContext context)

at
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext
context, WebServiceMethodData
methodData)","ExceptionType":"System.ArgumentException"}

Mi-24解决这个错误当有一个文本框的内容'字符


看看这个链接。

https://stackoverflow.com/a/493259/168371

您的WebService需要一个字符串而不是复杂的数据类型,因此您应该将JSON数据转换为一个字符串,然后传递它。

希望这有帮助!