所需工具与项目结构同(一)。

service.asmx中代码如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization; namespace WebService2
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempri/url")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{ [WebMethod(Description = "selectbyid")] public void getDBTableInfos(string EnterpriseCode)
{
HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
string jsonCallBackFunName = string.Empty;
jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
string jsonStr=string.Empty;
CarUsing caru = new CarUsing();
string sql = "select * from CarUsing where cuid =@cuid";
SqlParameter para = new SqlParameter("@cuid", Convert.ToInt32(EnterpriseCode));
using (SqlDataReader dr = SqlHelper.ExecuteReader(sql, CommandType.Text, para))
{
while (dr.Read())
{
caru = new CarUsing(
Convert.ToInt32(dr["cuid"]),
dr["carUsing"].ToString()
);
}
}
jsonStr = JsonConvert.SerializeObject(caru);
HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, new JavaScriptSerializer().Serialize(jsonStr)));
}
[WebMethod(Description = "测试selectall")]
public void getDBTableInfos1()
{
HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
string jsonCallBackFunName = string.Empty;
jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
string jsonStr = string.Empty;
List<CarUsing> CarUsings = new List<CarUsing>();
string sql = "select * from CarUsing order by cuid desc";
using (SqlDataReader dr = SqlHelper.ExecuteReader(sql, CommandType.Text))
{
while (dr.Read())
{
CarUsing carUsing = new CarUsing(
Convert.ToInt32(dr["cuid"]),
dr["carUsing"].ToString()
);
CarUsings.Add(carUsing);
}
jsonStr = JsonConvert.SerializeObject(CarUsings);
}
HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, new JavaScriptSerializer().Serialize(jsonStr)));
} [WebMethod(Description = "添加")]
public void getDBTableInfos2(string cusing)
{
string result = "";
HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
string jsonCallBackFunName = string.Empty;
jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
string jsonStr = string.Empty;
string sql = string.Format("insert into CarUsing values(@carUsing)");
SqlParameter para = new SqlParameter("@carUsing", cusing);
result = SqlHelper.ExecuteNonQuery(sql, CommandType.Text, para).ToString();
jsonStr = JsonConvert.SerializeObject(result);
HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, new JavaScriptSerializer().Serialize(jsonStr)));
}
[WebMethod(Description = "修改")]
public void getDBTableInfos3(string cuid,string cusing)
{
string result = "";
HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
string jsonCallBackFunName = string.Empty;
jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
string jsonStr = string.Empty;
string sql = string.Format("update CarUsing set carUsing =@carUsing where cuid=@cuid");
SqlParameter[] paras = {
new SqlParameter("@carUsing",cusing),
new SqlParameter("@cuid", cuid)
};
result = SqlHelper.ExecuteNonQuery(sql, CommandType.Text, paras).ToString();
jsonStr = JsonConvert.SerializeObject(result);
HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, new JavaScriptSerializer().Serialize(jsonStr)));
}
[WebMethod(Description = "删除")]
public void getDBTableInfos4(string cuid)
{
string result = "";
HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
string jsonCallBackFunName = string.Empty;
jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
string jsonStr = string.Empty;
string sql = string.Format("delete from CarUsing where cuid=@cuid");
SqlParameter para = new SqlParameter("@cuid", Convert.ToInt32(cuid));
result = SqlHelper.ExecuteNonQuery(sql, CommandType.Text, para).ToString();
jsonStr = JsonConvert.SerializeObject(result);
HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, new JavaScriptSerializer().Serialize(jsonStr)));
}
}
}

html中代码如下:

<!DOCTYPE html>

<html>

<head>

    <title>Index</title>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnSubmit").click(function () {
var EnterpriseCode = ""; //企业代码
var dataStr = "EnterpriseCode=" + EnterpriseCode;
$.ajax({
type: "get",
url: "http://localhost:22657/Service1.asmx/getDBTableInfos?jsoncallback?",
dataType: "jsonp",
jsonp: 'jsoncallback',
data: dataStr,
success: function (result) {
//返回结果
var str = result;
var jsonList = eval("(" + str + ")");
var html = "<table border=1 bordercolor=6d6d6d cellspacing = 1>";
html += "<tr backgroundcolor='yellow'><td>Id</td><td>用途</td><td>操作</td></tr>";
html += "<tr>";
html += "<td>" + jsonList.Cuid + "</td><td>" + jsonList.CareUsing + "</td><td><a href='javascript:;' onclick='UpdateInit(" + jsonList.Cuid + ")'>修改</a>&nbsp;<a href='javascript:;' onclick='if(confirm(\"确定删除嘛?\")){Delete(" + jsonList.Cuid + ");}'>删除</a></td>";
html += "</tr>";
html += "</table>"
$("#div1").html(html);
},
error: function (result) {
alert("error");
}
})
});
$("#btnSubmit1").click(function () {
$.ajax({
type: "get",
url: "http://localhost:22657/Service1.asmx/getDBTableInfos1?jsoncallback?",
dataType: "jsonp",
jsonp: 'jsoncallback',
data: "{}",
success: function (result) {
var str = result;
var jsons = eval("(" + str + ")");
var html = "<table border=1 bordercolor=6d6d6d cellspacing = 1>";
html += "<tr backgroundcolor='yellow'><td>Id</td><td>用途</td><td>操作</td></tr>";
for (var i = ; i < jsons.length; i++) {
html += "<tr>";
html += "<td>" + jsons[i].Cuid + "</td><td>" + jsons[i].CareUsing + "</td><td><a href='javascript:;' onclick='UpdateInit(" + jsons[i].Cuid + ")'>修改</a>&nbsp;<a href='javascript:;' onclick='if(confirm(\"确定删除嘛?\")){Delete(" + jsons[i].Cuid + ");}'>删除</a></td>";
html += "</tr>";
}
html += "</table>"
$("#div1").html(html); },
error: function (result) {
alert("error");
}
})
});
$("#btnSubmit2").click(function () {
var carUsing = "测试测试1"; //企业代码
var dataStr = "cusing=" + carUsing;
$.ajax({
type: "get",
url: "http://localhost:22657/Service1.asmx/getDBTableInfos2?jsoncallback?",
dataType: "jsonp",
jsonp: 'jsoncallback',
data: dataStr,
success: function (result) {
if (result = "") {
alert("success");
}
else {
alert("fail");
}
},
error: function (result) {
alert("error");
}
})
}); $("#btnSubmit3").click(function () {
var carUsing = "测试测试"; //企业代码
var dataStr = "cuid=5&cusing=" + carUsing;
$.ajax({
type: "get",
url: "http://localhost:22657/Service1.asmx/getDBTableInfos3?jsoncallback?",
dataType: "jsonp",
jsonp: 'jsoncallback',
data: dataStr,
success: function (result) {
if (result = "") {
alert("success");
}
else {
alert("fail");
}
},
error: function (result) {
alert("error");
}
})
}); $("#btnSubmit4").click(function () {
var carUsing = "";
var dataStr = "cuid=" + carUsing;
$.ajax({
type: "get",
url: "http://localhost:22657/Service1.asmx/getDBTableInfos4?jsoncallback?",
dataType: "jsonp",
jsonp: 'jsoncallback',
data: dataStr,
success: function (result) {
if (result = "") {
alert("success!");
}
else {
alert("fail");
}
},
error: function (result) {
alert("error");
}
})
});
});
</script>
</head> <body>
<div>
<input id="btnSubmit" type="button" value="selectbyId" />
<input id="btnSubmit1" type="button" value="selectall" />
<input id="btnSubmit2" type="button" value="Insert" />
<input id="btnSubmit3" type="button" value="Update" />
<input id="btnSubmit4" type="button" value="Delete" />
<div id="div1"></idv>
</div>
</body> </html>

Note:如果读取的是access数据库,在发布后记得设置access的写入权限。否则无法读取数据。

ajax调用webservice(二) 跨域。的更多相关文章

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

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

  2. ajax调用WebService 不能跨域

    http://www.cnblogs.com/dojo-lzz/p/4265637.html "Access-Control-Allow-Origin":'http://local ...

  3. ArcGIS API for Silverlight 调用WebService出现跨域访问报错的解决方法

    原文:ArcGIS API for Silverlight 调用WebService出现跨域访问报错的解决方法 群里好几个朋友都提到过这样的问题,说他们在Silverlight中调用了WebServi ...

  4. ajax调用webservice 跨域问题

    用js或者jquery跨域调用接口时 对方的接口需要做jsonp处理,你的ajax jsonp调用才可以 egg 接口中已经做了jsonp处理,所以可以跨域调用 //$.ajax({ // url: ...

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

  6. AJAX跨域问题解决方法(3)——被调用方解决跨域

    被调用方解决跨域是指在HTTP响应头中增加指定的字段,允许调用方调用 可以在两种地方增加1.apache/nginx(HTTP服务器)2.tomcat(应用服务器) 浏览器如何判断跨域?仔细观察可以发 ...

  7. AJAX跨域问题解决方法(4)——调用方解决跨域

    调用方解决跨域的方法只有一种,那就是隐藏跨域. 何为隐藏跨域? 隐藏跨域的核心思路是通过反向代理隐藏跨域以欺骗浏览器 什么是反向代理?反向代理是指通过中间服务器使得访问同一个域名的两个不同url最终会 ...

  8. Ajax调用WebService(一)

    Ajax调用WebService(一) http://www.cnblogs.com/leslies2/archive/2011/01/26/1934889.html 分类: Ajax 使用技术 We ...

  9. WebService对跨域的支持

    WebService对跨域的支持 跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源, ...

  10. Ajax调用WebService接口样例

    在做手机端h5的应用时,通过Ajax调用http接口时没啥问题的:但有些老的接口是用WebService实现的,也来不及改成http的方式,这时通过Ajax调用会有些麻烦,在此记录具体实现过程.本文使 ...

随机推荐

  1. Web工程师的工具箱

    RequestBin:允许你创建一个URL,利用这款工具进行收集请求,然后通过个性化方式进行检查. Hurl:发出HTTP请求,输入URL,设置标题,查看响应,最后分享给其他人.类似的工具有:REST ...

  2. POJ3617 简单字符串

    三分之一的通过率的字符串 题意为,输入一个S串,有一个空串T.对S串有两种操作,一是取出S串的头放入T串的尾,二是取出S串的尾放入T串的尾.要求是要使得T串的字典序最小. 从题意来看是一个很明显的贪心 ...

  3. 转: memcpy的用法总结

    1.memcpy 函数用于 把资源内存(src所指向的内存区域) 拷贝到目标内存(dest所指向的内存区域):拷贝多少个?有一个size变量控制拷贝的字节数:函数原型:void *memcpy(voi ...

  4. keil C51绝对地址访问

    在利用keil进行8051单片机编程的时,常常需要进行绝对地址进行访问.特别是对硬件操作,如DA AD 采样 ,LCD 液晶操作,打印操作.等等.C51提供了三种访问绝对地址的方法: 1. 绝对宏 在 ...

  5. Tag标签系统设计

    转一篇关于tag的文章:  <Tagging: People-powered Metadata for the Social Web>出版于2008年,中文版译为<标签:标记系统设计 ...

  6. FFT多项式乘法加速

    FFT基本操作...讲解请自己看大学信号转置系列... 15-5-30更新:改成结构体的,跪烂王学长啊啊啊啊机智的static... #include<iostream> #include ...

  7. oracle索引总结

    简介 1.说明 1)索引是数据库对象之一,用于加快数据的检索,类似于书籍的索引.在数据库中索引可以减少数据库程序查询结果时需要读取的数据量,类似于在书籍中我们利用索引可以不用翻阅整本书即可找到想要的信 ...

  8. libvirt之virt-install

    在使用kvm命令建立虚拟机时每次都要输入很长的命令,容易出现输入错误,可以使用libvirt管理虚拟机,libvirt支持kvm,xen等主流虚拟机的管理,下面介绍一下利用libvirt管理虚拟机. ...

  9. Selenium IDE整理

    安装 Step1: 下载Firefox浏览器 http://www.firefox.com.cn/ Step2: 安装Selenium IDE插件 http://seleniumhq.org/down ...

  10. Selenium webdirver 操作浏览器

    打开浏览器 HtmlUnit Driver 优点:不会实际打开浏览器,运行速度很快. 缺点:对JavaScript的支持不够好,有时会捕获不到页面元素. 使用:WebDriver driver=new ...