2011-12-15 4 views
1
에 jQuery를 사용하여 SQL 서버에 데이터를 삽입

내 코드 :오류시 Asp.net

Test.aspx 파일 :

<%@ 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"> 
    <title>Untitled Page</title> 
    <script type="text/javascript" language="javascript" src="Scripts/jquery.js"></script> 
    <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"); 
        } 
       }); 
      });  
     });  
    </script> 
</head> 
<body> 
    <form id="form2" runat="server"> 
     <div style="width:60px;">Remark</div> 
     <asp:TextBox ID="remark" runat="server" TextMode="MultiLine" Height="50px" Width="295px"></asp:TextBox> 
     <button id='btn_save' type="button">Save</button>       
    </form> 
</body> 
</html> 

* JqueryFunction.aspx 파일의 코드 :

//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); 
    } 

난에 입력하면 remark textbox : "데이터베이스에 삽입 할 수 없습니다"라는 코드가 잘 작동합니다. 내가 입력 할 때 :

{"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)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

텍스트 상자의 내용이

+1

당신은 db에 값을 삽입하기 전에 먼저 값을 소독해야한다고 생각합니다. .NET에서 그렇게하는 방법을 모르지만 PHP에서 mysql_escape_string과 비슷하다고 생각합니다. – bondythegreat

답변

1

는 모습을 가지고 ' 문자가 때이 오류를 해결할 수있는 방법 "나는 데이터베이스에 삽입 할 수 없습니다",이 오류가 발생합니다 이 링크에서.

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

귀하의 웹 서비스는 문자열이 아닌 복합 데이터 유형 기대와 같은 당신은 그것을 통과 한 후 문자열로 JSON 데이터를 변환해야합니다.

희망이 도움이됩니다.

+0

고맙습니다. 해결되었습니다. Pankaj Kumar 대단히 감사합니다 – Hainlp

+0

@Hainlp : 귀하의 문제를 해결 한 답을 허용 된 대답으로 표시하십시오. –