ajax调用webservice注意事项

2017-10-21 22:40:58来源:CSDN作者:qq_35056027人点击

分享

1.weservice客户端需要注意(跨域请求)

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <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>

2.ajax请求

var soapdata='{"subject":"测试电话李明远","cicc_contactid": null,"cicc_direction":"10","ownerid":"A6B254F4-6A49-E611-80BE-005056BD7FD2","cicc_contacts_text":"白展堂","cicc_if_im": null,"scheduleend":"2017-10-10 09:00:00","schedulestart":"2017-10-10 08:00:00","cicc_requestdepartment": null,"cicc_talktime": null,"description": null,"cicc_phonenumber": null,"cicc_company": null,"cicc_source": null}';
var soap='<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body> <InsertPhonecall xmlns="http://tempuri.org/"><obj>'+soapdata+'</obj></InsertPhonecall></soap12:Body></soap12:Envelope>';
 $.ajax({
                type: "POST",
                url: "http://loacalhost/PortalService/PortalService.asmx?InsertPhonecall ",
                data:soap,
                contentType: "application/soap+xml",
                charset:"utf-8",
                cache: false,
                dataType:"xml",
                async: false,
                success: function (data) { debugger; alert(data.firstChild.textContent); },
                error: function (data) { console.log(data);}
            });

分析一波soap格式,注意在soap1.2版本中<soap12:Body><方法名 xmlns="http://tempuri.org/"><参数名>参数</参数名></方法名></soap12:Body>

另外可以参考网页中涉及的传输方法

SOAP 1.1

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /PortalService.asmx HTTP/1.1Host: localhostContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://tempuri.org/Hi"<?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>    <Hi xmlns="http://tempuri.org/">      <hi>string</hi>    </Hi>  </soap:Body></soap:Envelope>
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-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>    <HiResponse xmlns="http://tempuri.org/">      <HiResult>string</HiResult>    </HiResponse>  </soap:Body></soap:Envelope>

SOAP 1.2

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /PortalService.asmx HTTP/1.1Host: localhostContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Body>    <Hi xmlns="http://tempuri.org/">      <hi>string</hi>    </Hi>  </soap12:Body></soap12:Envelope>
HTTP/1.1 200 OKContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Body>    <HiResponse xmlns="http://tempuri.org/">      <HiResult>string</HiResult>    </HiResponse>  </soap12:Body></soap12:Envelope>

HTTP POST

以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /PortalService.asmx/Hi HTTP/1.1Host: localhostContent-Type: application/x-www-form-urlencodedContent-Length: lengthhi=string
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">string</string>


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台