使用JQuery的Ajax调用SOAP-XML Web Services(Call SOAP-XML Web Services With jQuery Ajax)(译+摘录)

假设有一个基于.Net的Web Service,其名称为SaveProduct

POST /ProductService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://sh.inobido.com/SaveProduct"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <SaveProduct xmlns="http://sh.inobido.com/">
            <productID>int</productID>
            <productName>string</productName>
            <manufactureDate>dateTime</manufactureDate>
        </SaveProduct>
    </soap:Body>
</soap:Envelope>

在客户端用jQuery的ajax调用的代码为

var productServiceUrl = ‘http://localhost:57299/ProductService.asmx?op=SaveProduct‘; // Preferably write this out from server side

function beginSaveProduct(productID, productName, manufactureDate){
  var soapMessage =
  ‘<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>   <SaveProduct xmlns="http://sh.inobido.com/">   <productID>‘ + productID + ‘</productID>   <productName>‘ + productName + ‘</productName>   <manufactureDate>‘ + manufactureDate + ‘</manufactureDate>   </SaveProduct>   </soap:Body>   </soap:Envelope>‘;

  $.ajax({
    url: productServiceUrl,
    type: "POST",
    dataType: "xml",
    data: soapMessage,
    complete: endSaveProduct,
    contentType: "text/xml; charset=\"utf-8\""
  });
  return false;
}

function endSaveProduct(xmlHttpRequest, status){
 $(xmlHttpRequest.responseXML)
    .find(‘SaveProductResult‘)
    .each(function() {
     var name = $(this).find(‘Name‘).text();
   });
}

说明:

  1. type:  发送的请求的类型-the type of request we‘re sending
  2. dataType:  发回的响应的类型- the type of the response will send back, 一般情况下可以是html, json等类型,但如果是一个SOAP web service,必须定义为 xml类型
  3. data:  发送给web service的实际数据
  4. complete:  function (XMLHttpRequest, textStatus) {  /* Code goes here */ }
  5. contentType: 请求的MIME content类型, 同理,如果是一个SOAP web service,必须定义为 “text/xml”
  6. endSaveProduct:现在XML数据就已经发送到web service了,一旦服务器server接受到请求并进行处理,调用endSaveProduct方法

如用jQuery处理传回的XML响应,必须理解SOAP reponse’s schema定义,SaveProduct的schema格式如下:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SaveProductResponse xmlns="http://sh.inobido.com/">
      <SaveProductResult>
        <ID>int</ID>
        <Name>string</Name>
        <ManufactureDate>dateTime</ManufactureDate>
      </SaveProductResult>
    </SaveProductResponse>
  </soap:Body>
</soap:Envelope>

传回的响应放在xmlHttpRequest的参数responseXML中,可以使用firebug或Dev. Tool查看,现在我们就可以使用jQuery来遍历该XML的节点并处理数据了。

摘录自:http://openlandscape.net/2009/09/25/call-soap-xm-web-services-with-jquery-ajax/

使用JQuery的Ajax调用SOAP-XML Web Services(Call SOAP-XML Web Services With jQuery Ajax)(译+摘录)

时间: 2024-08-06 01:06:13

使用JQuery的Ajax调用SOAP-XML Web Services(Call SOAP-XML Web Services With jQuery Ajax)(译+摘录)的相关文章

类型:Ajax;问题:ajax调用ashx参数获取不到;结果:ashx文件获取$.ajax()方法发送的数据

ashx文件获取$.ajax()方法发送的数据 今天在使用Jquery的ajax方法发送请求时,发现在后台中使用ashx文件无法接收到ajax方法中传递的参数,上网查了一下原因后发现了问题所在,原来是我在$.ajax方法中指明了"contentType: 'application/json; charset=utf8'",所以才导致了在ashx文件中处理请求时无法获取传递到服务器端的参数, 正确的写法如下: 1 $.ajax({ 2 url: '/Handler1.ashx?operF

ajax 调用 .net core WebAPI,报错 400 (Bad Request) Unexpected character encountered while parsing value

此文由博主前两天的提问及 dudu 的回答整理,地址:https://q.cnblogs.com/list/myquestion 情况说明 基于 .net core 写了一个 Web API,用 postman 测试时,能够 POST 返回数据,但是用 ajax 调用 API 时就会报错(400). 前端 ajax 调用报错 postman 调用正常 服务端相关代码 [HttpPost] public async Task<ActionResult<List<Flow>>&g

jQuery的ajax调用webservice返回XML数据传参错误

jQuery的ajax调用webservice返回XML数据传参错误: 有时候使用jquery的ajax调用带有参数的webservice返回XML格式输出的时候,会出现传参错误,当然错误的原因可能是多种多样的,下面就简单介绍一种. 一.错误代码: 1.ajax代码: $.ajax({ type:"post", url:"_service.asmx/getDataFromATable", data:" { tablename: temp }",

用jQuery的Ajax调用WCF服务编程心得

这两天在写基于WCF服务的后台框架,过程中遇到了一些挫折,经过努力全部解决了,在此分享给大家,使用的工具是Visual Studio 2013. 该后台需要支持通过json来传递和接收数据. 首先,说说搭建过程. 第一步:创建WCF服务应用程序项目WCF. 第二步,创建服务使用的数据类 using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Sch

Asp.net中JQuery、ajax调用后台方法总结

通过上一篇文章实例的实现,整个过程当中学习到很多知识点,了解了Jquery.Ajax在asp.net中的运用,加以总结,其实原理都是一样的,理解了一种,其他的注意很少的区别就可以了.灵活运用: 1.有参数的方法调用 示例代码如下:前台jQuery代码:$(function() {  知道的一种就是ajax调后台的方法. 1.有参数的方法调用 示例代码如下: 前台jQuery代码: [plain] view plaincopyprint? <span style="font-size:18p

JQuery Ajax调用asp.net后台方法

出处:http://www.cnblogs.com/kingboy2008/ 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. 先来个简单的实例热热身吧. 1.无参数的方法调用 asp.net code: 1 2 3 4 5 6 7 using System.Web.Script.Services;      [WebMethod]   public static string SayHello()   {        return "Hello Ajax!&quo

Jquery利用ajax调用asp.net webservice的各种数据类型(总结篇)

原文:Jquery利用ajax调用asp.net webservice的各种数据类型(总结篇) 老话说的好:好记心不如烂笔头! 本着这原则,我把最近工作中遇到的jquery利用ajax调用web服务的各种数据类型做了一个总结! 本文章没有什么高难度技术,就是记录一下,汇总一下,以便以后需要时查看! 本总结牵涉的数据类型,主要有: string,int这样的基本数据类型 ClassA这样的自定义类 List<ClassA>这样的集合类型 Dictionary这样的字典类型数据 DataSet这样

[WebMethod]的使用,ajax调用[WebMethod]的使用,webservice(web服务) asmx的使用,ajax调用[WebMethod]进行json传输

首先,要分为.net 2.0和.net 3.5这两种情况来考虑 一:.net 2.0情况下 ajax可以调用 aspx.cs 里面加了 [webmethod]的静态方法,而不能调用 asmx 里面加了[webmethod]的方法或者是静态方法 调用aspx.cs里面的webmethod方法的时候,还必须在web.config里面添加如下代码 <httpModules> <add name="ScriptModule" type="System.Web.Han

Jquery Ajax调用aspx页面实例

目前,我会的几种asp.net界面与后台代码交互方式有几种: 1.webform+服务器控件交互: 2.webform+jquery+ajax+一般处理程序交互: 3.webform+jquery+ajax+Webservice/WCF交互: 4.MVC: 5.webform+jquery+ajax直接交互: 其中第1种交互是入门级,发展级为第2与第3,交互方式类似,也是我常用的开发方式.第4种最近几年才出现,玩过,用于项目比较少. 现在记录一下第5种交互方式. 第一步:准备页面代码: <!DO