2015-10-20 19 views
0

我有一个会话变量在我的web服务中登录,我将它与代码隐藏起来,但现在我需要再次使用jQuery来完成它。这是我的Web方法在web服务c中登录jQuery,对象json,ajax,变量会话#

[WebMethod(EnableSession = true)] 
    public Credencial SetSession(Credencial parametro) 
    { 
     RNLogin _login = new RNLogin(); 
     Credencial res = new Credencial(); 
     res.Nick = parametro.Nick; 
     res.Password = parametro.Password; 
     res.Identificador = _login.Login(res); 
     if(res.Identificador > 0) 
     { 
      HttpContext.Current.Session["Identificador"] = res.Identificador; 
     } 
     return res; 
    } 

在这里,我用AJAX的HTML代码,但此刻把我对象的JSON数据,它给我一个错误。

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title></title> 
    <script src="Scripts/jquery-1.11.3.js"></script> 
    <script type="text/javascript"> 
     jQuery(document).ready(function() { 
      $('#btnPrueba').click(function() { 
       var nombre = $('#txtNombre').val(); 
       var paterno = $('#txtPaterno').val(); 

       MiFuncionObj(nombre, paterno); 

      }); 
     }); 

     function MiFuncionObj(nick, password) { 
      // alert("nick = " + nick + ",pass =" + password) 
      //var actiondata = "{'nombre': '" + nombre + "','Paterno':'" + paterno + "'}"; 
      var objJson = { 
       "Nick": nick, "Password": password 
      } 

      var actiondata = JSON.stringify(objJson); 

      $.ajax({ 
      url: "wsSesion.asmx/SetSession", 
      // data: actiondata, 
      data: "{'parametro':" + '<%= actiondata %>' + "}", //objeto json con el nombre 
      dataType: "json", 
      type: "POST", 
      contentType: "application/json; utf-8", 
       // success: function (msg) { alert(msg.d); }, 
      success: function (msg) { 
       if (msg.d.Identificador > 0) { 
        alert("TU Nick: " + msg.d.Nick + ", TU Identificador: " + msg.d.Identificador); 
        window.location.href = "Acceso.aspx"; 
       } 
       else { 
        alert("Usuarios y/o password incorectos"); 
       } 
      }, 
      error: function (result) { 
       alert("ERROR" + result.status + '' + result.statusText); 
      } 
      }); 
     } 
    </script> 
</head> 
<body> 
    Nombre: <input type="text" id="txtNombre"/><br /> 
    Paterno: <input type="text" id="txtPaterno"/><br /> 

    <input type="button" id="btnPrueba" value="Probando JQuery" /> 

</body> 
</html> 
+0

请编辑您的问题关于:有什么错误?你在想什么?你卡在哪里? – 2015-10-20 17:53:08

+0

错误说名称'actiondata'不存在于我的实际上下文中,我试图做一个登录,但与jQuery,没有代码 –

回答

0
[System.Web.Services.WebMethod] 
public static Credencial SetSession(string nick, string password) 
{ 
    // do stuff... 
} 

脚本:

<script> 
var objJson = {}; 
objJson.nick = 'xxx'; 
objJson.password = 'xxx'; 

$.ajax({ 
url: "wsSesion.asmx/SetSession", 
      data: JSON.stringify(objJson), 
      dataType: "json", 
      type: "POST", 
      contentType: "application/json; utf-8", 
       // success: function (msg) { alert(msg.d); }, 
      success: function (msg) { 
       if (msg.d.Identificador > 0) { 
        alert("TU Nick: " + msg.d.Nick + ", TU Identificador: " + msg.d.Identificador); 
        window.location.href = "Acceso.aspx"; 
       } 
       else { 
        alert("Usuarios y/o password incorectos"); 
       } 
      }, 
      error: function (result) { 
       alert("ERROR" + result.status + '' + result.statusText); 
      } 
}); 
</script> 
+0

多数民众赞成在罚款,但我想这样做与数据库值,包括一个存储过程 –