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="" 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]前的注释。
asp.net ajax 调用错误解决的更多相关文章
- Ajax 调用webservice 解决跨域请求和发布到服务器后本地调用成功外网失败的问题
webservice 代码 /// <summary> /// MESService 的摘要说明 /// </summary> [WebService(Namespac ...
- asp.net Ajax调用Aspx后台方法
Ajax调用的前提(以aspx文件为例:) 1.首先需要在aspx文件后台中引用using System.Web.Services; 2.需要调用的方法必须是公共的(public).静态的(stati ...
- asp.net ajax 调用后台方法
js代码 <form id="form1" runat="server"> <script language=javascript type= ...
- ASP.NET AJAX调用 WebService
同事的代码,帮忙修改的,为了实现页面跳转回来后,状态的保持,Service 使用了Session. 主要的JS $.ajax({ url: "/ws/StaffInfo.asmx/Note& ...
- asp.net ajax 调用一例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- Ajax调用处理页面错误信息500的解决思路
最近在做项目的时候遇到一个问题:(李昌辉) 在本地服务器上做好之后,部署到阿里云虚拟主机,结果访问页面出现问题,由于登录使用的是AJAX调用处理页面,所以在点击登录的时候没有任何反应. 打开F12调试 ...
- Asp.Net MVC ajax调用 .net 类库问题
如果你还在为 ajax 调用 .net 类库还束手无策的话,相信这篇博客将帮助你解决这个世纪问题! 因为Visual Studio 内置了asp.net mvc ,不过当你添加asp.net mvc项 ...
- [置顶] Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)
无论在Window应用程序,还是Web应用程序以对用户友好的方式显示运行时的异常都是很有必要,尤其对于可能有很多不确定因素导致异常的Web应用程序;在传统的Web开发中,处理异常的方式——设计专门一个 ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
随机推荐
- Win7中使用Eclipse连接虚拟机中的Ubuntu中的Hadoop2.4<3>
经过前几天的学习,基本上能够小试牛刀编写一些小程序玩一玩了,在此之前做几项准备工作 明白我要用hadoop干什么 大体学习一下mapreduce ubuntu重新启动后,再启动hadoop会报连接异常 ...
- CSDN问答频道“华章杯”7月排行榜活动开始,丰厚奖品等你拿
CSDN问答频道月度排行榜,是CSDN问答频道从3月开始举办的活动,旨在鼓励更多用户参与提问和解答,创造一个良好的互帮互助氛围,使参与者在问和答的过程中得到技术水平的提升,也希望大家能在技术交流中结交 ...
- 【M25】将构造方法和非成员方法虚化
1.所谓虚化,就是根据引用或者指针的真实类型,决定调用哪个方法. 2.构造方法虚化,就是根据引用(或者指针)的真实类型,构造出一个对象,如果指针的真实类型是Base,返回Base*:如果指针的真实类型 ...
- VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟
C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...
- 支付宝api指南
tyle="margin:20px 0px 0px; line-height:26px; font-family:Arial"> 在这些服务中,服务类型大致可以分为以下几类: ...
- Effective C++笔记04:设计与声明
条款18:让接口easy被正确使用,不易被误用 1,好的接口非常easy被正确使用,不easy被误用.你应该在你的全部接口中努力达成这些性质. 2,"促进正使用"的办法包含接口的一 ...
- Nginx_handler模块发开(hello模块结构解析)
声明:请在文章页面明显位置给出原文连接 http://www.cnblogs.com/paulweihan/p/4654173.html,否则保留追究法律责任的权利. 近期查了非常多资料.入门的样例都 ...
- android-square-progressbar-legacy
https://github.com/eltld/android-square-progressbar-legacy
- iOS开发——UI篇Swift篇&UIDatePicker
UIDatePicker //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControlle ...
- 死锁相关 变量 与 PURGE 线程停止
http://www.tuicool.com/articles/NzAFZn https://github.com/percona/percona-server/pull/83/commits/091 ...