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 调用错误解决的更多相关文章

  1. Ajax 调用webservice 解决跨域请求和发布到服务器后本地调用成功外网失败的问题

        webservice 代码 /// <summary> /// MESService 的摘要说明 /// </summary> [WebService(Namespac ...

  2. asp.net Ajax调用Aspx后台方法

    Ajax调用的前提(以aspx文件为例:) 1.首先需要在aspx文件后台中引用using System.Web.Services; 2.需要调用的方法必须是公共的(public).静态的(stati ...

  3. asp.net ajax 调用后台方法

    js代码 <form id="form1" runat="server"> <script language=javascript type= ...

  4. ASP.NET AJAX调用 WebService

    同事的代码,帮忙修改的,为了实现页面跳转回来后,状态的保持,Service 使用了Session. 主要的JS $.ajax({ url: "/ws/StaffInfo.asmx/Note& ...

  5. asp.net ajax 调用一例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  6. Ajax调用处理页面错误信息500的解决思路

    最近在做项目的时候遇到一个问题:(李昌辉) 在本地服务器上做好之后,部署到阿里云虚拟主机,结果访问页面出现问题,由于登录使用的是AJAX调用处理页面,所以在点击登录的时候没有任何反应. 打开F12调试 ...

  7. Asp.Net MVC ajax调用 .net 类库问题

    如果你还在为 ajax 调用 .net 类库还束手无策的话,相信这篇博客将帮助你解决这个世纪问题! 因为Visual Studio 内置了asp.net mvc ,不过当你添加asp.net mvc项 ...

  8. [置顶] Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)

    无论在Window应用程序,还是Web应用程序以对用户友好的方式显示运行时的异常都是很有必要,尤其对于可能有很多不确定因素导致异常的Web应用程序;在传统的Web开发中,处理异常的方式——设计专门一个 ...

  9. 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 ...

随机推荐

  1. 理解reserve与resize

    1.首先明白capacity与size的概念,capacity表示当前可以容纳多少个元素,size表示当前有多少个元素.为了避免频繁地分配内存,vector预留了一些内存.也就是说:size<= ...

  2. Swift学习笔记十

    枚举 一个枚举为一组相关联的值定义一个通用类型,并且让你可以在代码中类型安全地操作这些值. C中的枚举将关联的名字指派给一系列整型值.Swift中的枚举类型更为活泼,并不需要为每个成员指定值,如果指定 ...

  3. cdoj 80 Cube 水题

    Cube Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/80 Descrip ...

  4. 绝对好文:.NET程序性能的基本要领

    Bill Chiles(Roslyn编译器的程序经理)写了一篇文章<Essential Performance Facts and .NET Framework Tips>,知名博主寒江独 ...

  5. BBOSS框架使用jquery方式传參到后台的时候,要注意的事项

    BBOSS框架.从前台传到后台的时候,參数要以这样的方式: public String initAddOrModExtendUser(HttpServletRequest request,       ...

  6. vs2012运行c语言出现:无法查找或打开 PDB 文件

    1.点 调试2.然后 选项和设置3.右边勾上 启用源服务器支持4.左边点 符号5.把微软符号服务器勾6.运行的时候等一下 莫慌!!7.问题解决

  7. Mac OS X下高速拷贝文件路径

    文件路径表示的是文件在文件系统里的位置,无论是用命令行操作的Linux程序猿还是使用windows的广大用户.都难免会有这种需求 在windows上复制当前文件夹的路径有一个特别方便的方式,仅仅须要用 ...

  8. nginx平滑重启与平滑升级的方法

    如何实现nginx平滑重启与平滑升级? 平滑重启 kill -HUP `cat /usr/local/www/nginx/logs/nginx.pid` 平滑升级nginx: cd /yujialin ...

  9. linux下的十六进制编辑器---wxHexEdit

    ....其实wxHexEdit是一个跨平台的十六进制编辑器,支持windows,linux,mac. 之所以标题用linux...是因为windows下多数都用winhex,UE之类的编辑器,而lin ...

  10. JavaScript随机数

    function random(start,end){ var total=start+end; return Manth.floor(Manth.random()+total-start); }