数码产品

asp.net ajax 调用错误解决

ajax调用aspx页面出现如下错误

前台源代码:

<!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>
    <title></title>
     <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#save").click(function () {
                var user = {};
                user.name = $("#name").val();
                user.age = $("#age").val();
                user.sex = $("#sex").val();
                user.email = $("#email").val();
                user.phone = $("#phone").val();
                alert(user.name);
                alert(user.sex);
                alert(user.age);
                alert(user.email);
                alert(user.phone);
                alert('{user:' + JSON.stringify(user) + '}');
                $.ajax({
                    type: "POST",
                    url: "../Index.aspx/adduser",

                    data: '{user:' + JSON.stringify(user) + '}',
                    dataType: "json",
                    contentType: "application/json;",
                    success: function (result) {
                        alert("User has been added successfully.");
                       
                        //getDetails(); //This method is to bind the added data into my HTML Table through Ajax call instead of page load  
                        // window.location.reload(); we can also use this to load window to show updated data  
                    },
                    error: function (xhr) { document.write(xhr.responseText) }
                    //                    error: function () {
                    //                        alert("Error while inserting data");
                    //                        alert(Error.toString());
                    //                    }
                });
                return false;
            });
        });
       
    </script>
</head>
<body>

<div>
    
    <div>
    <span>姓名:</span><span><input id="name" name="name" type="text" placeholder="ss" required=""/></span>
    </div>
    <div>
    <span>年龄:</span><span><input id="age" name="age" type="text"/></span>
    </div>
    <div>
    <span>性别:</span><span><select id="sex" name="sex" required=""><option  value="" disabled="disabled">--select--</option><option value="man">男</option><option value="women">女</option></select></span>
    </div>
    <div>
    <span>邮件:</span><span><input id="email" name="email" type="text" placeholder="abc@xx.com" required=""/></span>
    </div>
    <div>
    <span>电话:</span><span><input id="phone" name="phone" type="text" placeholder="12345678901" required=""/></span>
    </div>
    <div>
    <span><input id="save" value="保存" type="button" /></span><span><input id="cancel" value="取消" type="button"/></span>
    </div>

</div>

</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;

namespace Web
{
    public partial class Index : System.Web.UI.Page
    {
       static  string sqlcon = System.Configuration.ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString.ToString();
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //[WebMethod]
        public  static void adduser(User user)
        {
            using (SqlConnection con = new SqlConnection(sqlcon))
            {
                using (SqlCommand cmd = new SqlCommand("insert into Tb_user(_name,_age,_sex,_email,_phone) values(@name,@age,@sex,@email,@phone)"))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@name", user.name);
                    cmd.Parameters.AddWithValue("@age", user.age);
                    cmd.Parameters.AddWithValue("@sex", user.sex);
                    cmd.Parameters.AddWithValue("@email", user.email);
                    cmd.Parameters.AddWithValue("@phone", user.phone);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    
                }
            }

        }
    }
}

错误解决办法是:把后台代码的方法名前加一个[WebMethod]即可。

ajax 调用asmx,出现错误提示,提示如下:

前台代码如上

后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Data;

namespace Web
{
    /// <summary>
    /// issue 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    //[System.Web.Script.Services.ScriptService]
    public class issue : System.Web.Services.WebService
    {
        string sqlcon = System.Configuration.ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString.ToString();
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public void adduser(User user) 
        {
            using (SqlConnection con = new SqlConnection(sqlcon)) 
            {
                using (SqlCommand cmd = new SqlCommand("insert into Tb_user(_name,_age,_sex,_email,_phone) values(@name,@age,@sex,@email,@phone)")) 
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@name", user.name);
                    cmd.Parameters.AddWithValue("@age", user.age);
                    cmd.Parameters.AddWithValue("@sex", user.sex);
                    cmd.Parameters.AddWithValue("@email",user.email);
                    cmd.Parameters.AddWithValue("@phone",user.phone);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }
    }
}

解决办法:

去掉[System.Web.Script.Services.ScriptService]前的注释。

 

posted @ 2015-11-25 15:21  Hackerman  阅读(639)  评论(0编辑  收藏  举报
数码产品