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 ...
随机推荐
- linux C(hello world) 二维数组的练习
- 摘录知乎上关于android开发的话题精华
1.初学者学习 Android 开发,有什么好网站推荐? http://www.zhihu.com/question/19611325 2.Android 开发有哪些新技术出现? http://www ...
- IOS触摸事件和手势识别
IOS触摸事件和手势识别 目录 概述 触摸事件 手势识别 概述 为了实现一些新的需求,我们常常需要给IOS添加触摸事件和手势识别 触摸事件 触摸事件的四种方法 -(void)touchesBegan: ...
- 过滤文本文档中的数据并插入Cassandra数据库
代码如下: package com.locationdataprocess; import java.io.BufferedReader; import java.io.File; import ja ...
- 错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法
编写布局代码时发现xml脚本出现错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”,原来是一个na ...
- AlloyTouch实现下拉刷新
原文地址:https://github.com/AlloyTeam/AlloyTouch/wiki/Pull-to-refresh 效果展示 扫码体验 你也可以点击这里访问Demo 可以点击这里查看代 ...
- 修改FFMpeg源码—捕获丢包
概述 最近我们项目有一个需求就是解决客户端播放RTSP视频流花屏的问题,一般来说丢包就会引起花屏,导致客户端花屏的因素又有很多,比如说: 相机到服务器丢包 服务器到客户端丢包 等等... 其中服务器到 ...
- sql查询行转列
昨天下午碰到一个需求,一个大约30万行的表,其中有很多重复行,在这些行中某些字段值是不重复的. 比如有ID,NAME,CONTRACT_id,SALES,PRODUCT等,除了PRODUCT字段,其余 ...
- 适配iOS9遇到的一些问题_Scheme白名单_ Bitcode及解决办法
升级Xcode7 运行项目发现报错如下: 1.Scheme白名单问题 -canOpenURL: failed for URL: “weixin://app/wxdaae92a9cfe5d54c/” - ...
- C#.net 获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...