asp.net ajax 调用错误解决
2015-11-25 15:21
537 查看
ajax调用aspx页面出现如下错误
![](https://images2015.cnblogs.com/blog/566138/201511/566138-20151125151202406-2040196947.png)
前台源代码:
后台代码:
错误解决办法是:把后台代码的方法名前加一个[WebMethod]即可。
ajax 调用asmx,出现错误提示,提示如下:
![](https://images2015.cnblogs.com/blog/566138/201511/566138-20151125151825656-1219621084.png)
前台代码如上
后台代码如下:
解决办法:
去掉[System.Web.Script.Services.ScriptService]前的注释。
![](https://images2015.cnblogs.com/blog/566138/201511/566138-20151125151202406-2040196947.png)
前台源代码:
<!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,出现错误提示,提示如下:
![](https://images2015.cnblogs.com/blog/566138/201511/566138-20151125151825656-1219621084.png)
前台代码如上
后台代码如下:
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]前的注释。
相关文章推荐
- ASP.NET中控件命名规则
- asp.net mvc实现rest风格返回json
- asp.net mvc实现rest风格返回json
- Asp.Net 之 抓取网页内容
- asp.net,cookie,写cookie,取cookie
- Asp.Net完美隐藏服务器信息
- asp.net缓存(转)
- asp.net实现Gradview绑定数据库数据并导出Excel的方法
- asp.net简单生成验证码的方法
- asp.net基于windows服务实现定时发送邮件的方法
- ASP.NET 之 网页快照 (DrawToBitmap)
- 让 Parse Double 漏洞无处藏身,工程师们必备神器!
- Asp.Net 之 网页快照
- asp.net基于session实现购物车的方法
- 动态网站开发技术学习4:ASP.NET内置对象之Request对象
- asp.net编程实现删除文件夹及文件夹下文件的方法
- asp.net类序列化生成xml文件实例详解
- ASP.NET AjaxControlToolkit-Framework4.0 配置实用(简单介绍CalendarExtender日期控件)
- 重新理解:ASP.NET 异步编程(转)
- asp.net mvc4 System.Web.Optimization找不到引用