jquery调用webservice

1.给webserver加 [System.Web.Script.Services.ScriptService]标签
如:
Code

2.js
   $(
        
function()
        {
            
function WebService(url,pars,callback)
            { 
                $.ajax
                ({ 
                    async :
false,
                    data:pars, 
                    url: url, 
                    type: 
"POST"
                    contentType: 
"application/json;utf-8"
                    dataType:
'json'
                    cache: 
false
                    success: 
function(json){ 
                        callback(json);
                    },
                    beforeSend:
function(xml)
                    { 
                        
if(!pars) 
                        {
                            xml.setRequestHeader(
"Content-Type""application/json;utf-8")
                        } 
                    },
                    error:
function(xml,status)
                    { 
                        
if(status=='error')
                        { 
                            
try
                            { 
                                
var json = eval('(' + xml.responseText + ')'); 
                                alert(
'Err:' + json.Message+'\n'+json.StackTrace); 
                            }
                            
catch(e)
                            {} 
                        }
                        
else
                        { 
                            alert(
'Err:' + status); 
                        } 
                    }                           
                });
            }
            
            $(
"#btnLogin").click
            (
                
function()
                {
                   
var n,p,r;
                    n
=$("#txtUsername").val();
                    p
=$("#txtPassword").val();
                    
if(n.length==0 || p.length==0)
                    {
                        alert(
"Please input user name and password.");
                        $(
"#txtUsername")[0].focus();
                        
if(n.length>0)
                            $(
"#txtPassword")[0].focus();
                         
return false;
                    }
                    WebService(
                        
"user.asmx/Login",
                        
"{name:'" + n + "',pwd:'" + p + "'}",
                        
function(jason)
                        {
                            
if(!jason)
                            {
                                alert(
"User name or password error,please retry.");
                                $(
"#txtPassword")[0].focus();
                                r 
= false;
                            }
                            
else
                            {
                                r 
= true;
                            }
                        }
                    );
                    
if(r)
                        window.location.href
="/MainForm.aspx";
                    
return false;
                }            
            )
        }
    )
posted @ 2009-09-07 14:04  zyip  阅读(194)  评论(0编辑  收藏  举报