js调用Webservice接口案例
第一步:新建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接口案例的更多相关文章
- 使用JS调用WebService接口
<script> $(document).ready(function () { var username = "admin"; var password = &quo ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
- php中创建和调用webservice接口示例
php中创建和调用webservice接口示例 这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservi ...
- 使用soapui调用webservice接口
soapui是专门模拟调用webservice接口的工具,下面介绍下怎么使用: 1.下载soapui并安装: 2.以免费天气获取接口为例:http://www.webservicex.net/glob ...
- JS调用WebService,发布到IIS,网页提示WebService未定义[已解决]
VS2013中,JS调用WebService,一直运行正常.部署到WindowsServer2008之后,在网页中访问,始终提示网页中有错误,点开之后发现是WebService未定义. 于是上网查解决 ...
- Js 调用 WebService 实例
Html页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/ ...
- java 调用webservice接口wsdl,推荐使用wsdl2java,放弃wsimport
网上说wsimport是jdk1.6后自带的客户端生成调用webservice接口的工具,其实我挺喜欢原生的东西,毕竟自家的东西用着应该最顺手啊,但往往让人惊艳的是那些集成工具. 本机jdk1.8.1 ...
- asp.net 练习 js 调用webservice
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- SQL调用WebService接口
今天在做一个非常奇葩的东西.中间有个过程要在SQL触发器里面调用webservice接口.呵呵~ ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFT ...
随机推荐
- JavaScript面试技巧(三):开发环境、运行环境
一.开发环境 1 介绍 2.IDE 3 .git - 4.模块化 - AMD CommonJS 5.构建工具 安装nodejs 安装webpack 配置webpack 使用jquery 压缩JS 6. ...
- Java基础_0306:数组的定义与使用
数组 数组指的就是一组相关变量的集合.例如:如果说现在要想定义100个整型变量,按照传统的思路,可能这样定义: int i1,i2 ,... i100,一共写100个变量. 以上的形式的确可以满足技术 ...
- Webstorm通用设置
常用快捷键 Ctrl+Alt+L : 格式化代码
- Css - 精灵图
Css - 精灵图css sprite 一个页面文档上总是会有N多的图标小图片,它们都是以背景图的方式嵌入文档,每个小图片需要一个url的css属性,每个url都指向一个服务器地址的链接,每个链接都代 ...
- Django实战(一)-----用户登录与注册系统3(前端页面、登录视图)
基本框架搭建好了后,我们就要开始丰富页面内容了.最起码,得有一个用户登录的表单不是么?(注册的事情我们先放一边.) 一. 原生HTML页面 删除原来的login.html文件中的内容,写入下面的代码: ...
- openstack Q版部署-----nova服务配置-控制节点(5)
一.创建数据库(控制节点) 创建数据库以及用户: CREATE DATABASE nova_api; CREATE DATABASE nova; CREATE DATABASE nova_cell0; ...
- 使用Swagger自动生成API文档
⒈添加pom依赖 <!-- Swagger核心包,用于扫描程序生成文档数据 --> <dependency> <groupId>io.springfox</g ...
- SpringMVC中前后端Date交互
建议使用时间戳来避免日期格式带来的前后端问题,使用时间戳可以避免前后端.平台等问题. new Date().getTime()
- 应用 XAF 开发移动手机应用
应用 XAF 开发移动手机应用: 1. How to create a native mobile or lightweight web client UI based on the existing ...
- 使用unix的time命令进行简单的计时
/usr/bin/time -p python3 1.py 需要注意这里使用的是/usr/bin/time 命令而不unix系统自带time. 输出 real 0.04 user 0.03 sys 0 ...