第一步:新建Webservice接口

主文件方法

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace TestWebApplication
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://10.12.1.90:5555/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]//(AJAX调用必须放开注释,net framwork 4.0版本及更高版本)
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
var ss = Context.Request.QueryString["fname"];
return "Hello World"+ ss;
}

[WebMethod]
public string HelloWorld1(string ww)
{

Context.Response.Charset = "GB2312"; //设置字符集类型
Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Context.Response.Write("success");
Context.Response.End();

}
}

配置文件Web.config

<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<!--
有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。

可在 <httpRuntime> 标记上设置以下特性。
<system.Web>
<httpRuntime targetFramework="4.5" />
</system.Web>
-->
<system.web>
<!--请求设置-->
<httpRuntime maxRequestLength="204800" executionTimeout="3600"/>
<compilation debug="true" targetFramework="4.5"/>
<!--设置允许http,post,get方式请求-->
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
<!--<compilation debug="true" targetFramework="4.0" />-->
<!--
通过 <authentication> 节,可配置
ASP.NET 用于识别进入用户的
安全身份验证模式。
-->
<authentication mode="Windows"/>
<!--
通过 <customErrors> 节,可以配置
在执行请求的过程中出现未处理的错误时要执行
的操作。具体而言,
开发人员通过该节可配置要显示的 html 错误页,
以代替错误堆栈跟踪。

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>

第二步:前台js通过ajax调用Webservice接口(本文件是一个html文件,包含一个按钮,点击按钮,js通过ajax调用Webservice接口)

<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
var XMLHttpReq;
function createXMLHttpRequest() {
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); //IE高版本创建XMLHTTP
}
catch (E) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); //IE低版本创建XMLHTTP
}
catch (E) {
XMLHttpReq = new XMLHttpRequest(); //兼容非IE浏览器,直接创建XMLHTTP对象
}
}
}

function sendAjaxRequest(url) {
//alert(url);
var json;
createXMLHttpRequest();
XMLHttpReq.open("post", url, false);
XMLHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//请求头文件设置编码
XMLHttpReq.onreadystatechange = function () {
if (XMLHttpReq.readyState == 4) {
if (XMLHttpReq.status == 200) {
document.getElementById("myDiv").innerHTMLXMLHttpReq.responseText

json = eval("(" + XMLHttpReq.responseText + ")");

}
}
}
XMLHttpReq.send("ww=123");//参数

return json;
}
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("post","http://localhost:58081/WebService1.asmx/HelloWorld1",false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("ww=123");

}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
<body>
<input id="Button1" type="button" onclick="sendAjaxRequest('http://localhost:58081/WebService1.asmx/HelloWorld1')" value="button" />

</body>
</body>
</html>

js调用Webservice接口案例的更多相关文章

  1. 使用JS调用WebService接口

    <script> $(document).ready(function () { var username = "admin"; var password = &quo ...

  2. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  3. php中创建和调用webservice接口示例

    php中创建和调用webservice接口示例   这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservi ...

  4. 使用soapui调用webservice接口

    soapui是专门模拟调用webservice接口的工具,下面介绍下怎么使用: 1.下载soapui并安装: 2.以免费天气获取接口为例:http://www.webservicex.net/glob ...

  5. JS调用WebService,发布到IIS,网页提示WebService未定义[已解决]

    VS2013中,JS调用WebService,一直运行正常.部署到WindowsServer2008之后,在网页中访问,始终提示网页中有错误,点开之后发现是WebService未定义. 于是上网查解决 ...

  6. Js 调用 WebService 实例

    Html页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/ ...

  7. java 调用webservice接口wsdl,推荐使用wsdl2java,放弃wsimport

    网上说wsimport是jdk1.6后自带的客户端生成调用webservice接口的工具,其实我挺喜欢原生的东西,毕竟自家的东西用着应该最顺手啊,但往往让人惊艳的是那些集成工具. 本机jdk1.8.1 ...

  8. asp.net 练习 js 调用webservice

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. SQL调用WebService接口

    今天在做一个非常奇葩的东西.中间有个过程要在SQL触发器里面调用webservice接口.呵呵~ ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFT ...

随机推荐

  1. luogu 4377 Talent show 01分数规划+背包dp

    01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...

  2. 遍历页面上的checkbox

    $("#Button1").click(function () { $("input[type='checkbox']").each(function () { ...

  3. 隐马尔可夫模型HMM(二)概率计算问题

    摘自 1.李航的<统计学习方法> 2.http://www.cnblogs.com/pinard/p/6955871.html 一.概率计算问题 上一篇介绍了概率计算问题是给定了λ(A,B ...

  4. linux 测试 get 请求 跳过SSL证书验证

    Linux 下测试 get 请求: curl : curl "http://www.qq.com" # 标准输出页面内容 curl -i "http://www.qq.c ...

  5. 对div的操作

    链接:https://blog.csdn.net/wide288/article/details/34116495 判断DIV的内容改变的方法 近日,在帮朋友写一段小程序的时候,用到了iframe,在 ...

  6. Kaldi语料的两种切分/组织方式及其处理

    text中每一个文本段由一个音频索引(indexed by utterance) 使用该方式的egs:librispeech.timit.thchs30.atc_en.atc_cn 语料的组织形式为: ...

  7. 【css】正确的让文本换行

    通常文本换行我们会使用word-break属性,有两个值供我们选择 word-break: break-all; word-break: break-world; 那么如何区分这两个值呢,我们看图说话 ...

  8. yum 记一次安装时的报错

    我电脑是centos 6.8,我先安装了openslp-2.0.0-3.el6.x86_64.rpm 然后我更改了yum源配置文件,我将updates源给禁用了,只保留os源和extras源,注意了, ...

  9. 小程序view排版

    <view class="bc"> <view> <text bc_text>demo</text> </view> & ...

  10. Linux 磁盘分区,文件系统创建、挂载、开机自动挂载和卸载

    创建分区 (fdisk): 第一步先在Linux的虚拟机上添加一块硬盘,添加完成后需要重启虚拟机才能够检测识别到新硬盘. 重启系统后可以使用 fdisk -l 命令查看当前所有磁盘分区情况,sdb为我 ...