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 ...
随机推荐
- Android Studio开发JNIproject
使用Android Sutdio创建一个新的project后,接下来记录创建NDKproject的基本步骤. 本文将达到: 1. 创建NDKproject 2. 在JNI中输出Log语句 3. 指定编 ...
- tomcat解压war包的一点例外
我在项目的开发过程中,发现Tomcat解压war 的一点例外. 现象如下: 使用ANT工具把web应用程序打包为war文件.然后把war文件放到tomcat的webapps,让tomca ...
- 我的git使用记录
git的教程现在琳琅满目,需要学习的东西也有很多,一下子接受不了那么多的东西,所以打算记录在实用的过程中常用的操作和遇到的问题. 基本操作 git init git add . git add -A ...
- [C++基础]随机数,随机种子数
#include <stdlib.h> #include <iostream> #include <ctime> using namespace std; void ...
- php heredoc 与 nowdoc
php heredoc 与 nowdoc heredoc 结构 heredoc 句法结构:<<<.在该运算符之后要提供一个标识符,然后换行.接下来是字符串本身,最后要用前面定义的标识 ...
- Mysql子查询IN中使用LIMIT
学习下Mysql子查询IN中使用LIMIT的方法. 这两天项目里出了一个问题,mysql LIMIT使用后报错. 需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料 ...
- qregularexpression和qregexp的区别
QRegularExpression 是Qt 5.0才引进的,相对于QRegExp,QRegularExpression class修复了很多bug,提高了效率,提供了对Perl的RegEx几乎全面兼 ...
- npm常用命令->nodejs
npm install <name>安装nodejs的依赖包 例如npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本, ...
- Robot Framework简介
概述 Robot Framework是一个通用的关键字驱动自动化测试框架.测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储. 通过测试库中实现的关键字驱动被测软件.Robot Fra ...
- 在window平台搭建Qt开发环境(使用VS2008 IDE)
一直用QT Creator(mingw)开发Qt应用程序,每次如果需要修改编译链接参数选项时,都要修改pro文件,而这个文件是基于文本的,每次都要记住这些选项参数名,如果在知道原理的情况下还记住这些字 ...