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. CentOS6.5下安装MariaDB5.5.36

    yum groupinstall -y "Development Tools" yum install -y cmake openssl-devel zlib-devel yum ...

  2. 标准C++中的STL容器类简单介绍

    SGI -- Silicon Graphics[Computer System] Inc.硅图[计算机系统]公司. STL -- Standard Template Library 标准模板库.   ...

  3. 从零开始学android开发-项目打包发布

    右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...

  4. PHP apache2.2 mysql 的安装

    1.安装准备 我的系统为win7 旗舰版 php:http://windows.php.net/downloads/releases/php-5.3.6-Win32-VC9-x86.zip mysql ...

  5. VS环境下搭建自己NuGet服务器

    一.NuGet服务端的搭建 环境:.NET 4.5 + VS2015 + NuGet.Server 2.10.1 1.建一个空的Web项目,取名叫NuGetServer 2.通过NuGet安装NuGe ...

  6. Busybox支持中文的解决办法

    转载:http://blog.csdn.net/wavemcu/article/details/7202908 ******************************************** ...

  7. Emmet语法

    子代:> 如:div>ul>li <div> <ul> <li> </li> </ul> </div> 兄弟: ...

  8. WPF 之 未捕获异常的处理

    首先,我们当然是要求应用程序开发人员,尽可能地在程序可能出现异常的地方都去捕捉异常,使用try…catch的方式.但是总是有一些意外的情况可能会发生,这就导致会出现所谓的“未捕获异常(Unhandle ...

  9. python3.x爬取美团信息

    在之前的文章中,笔者有提到,我们要在实践中去学习python,笔者有天就想着要不要爬点东西呢,跃跃欲试的节奏啊,想来想去,想到美团了,那么首先笔 者想给自己确定一个目标,就是我要爬什么样的数据,我要爬 ...

  10. 微信js获得签名signature

    服务器端: 1 获取微信js accessToken 备注:access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 开发者需要进行妥善保存.access_ ...