Asynchronous call WebService return responsexml for empty Problem Solving method _ Practical Tips

Source: Internet
Author: User
Tags soap xmlns
summarize some essentials first
1 to be familiar with JavaScript to the XML file loading and operation;
Examples of the XML operations of the DOM can be referenced: http://www.w3school.com.cn/xmldom/met_document_getelementsbytagname.asp
2) under IE or through loadxml to turn responsetext;
3 The XML after loading asynchronous property settings;
4 namespace processing and other issues;
The following code:
========aspx Foreground Code ========
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div id= "Div1" >
</div>
</form>
<p><input id= "Button1" type= "button" value= "button" onclick= "Requestwebservice ();"/></p>
<script type= "Text/javascript" >
var susragent = navigator.useragent;
var Isie = Susragent.indexof ("MSIE")!=-1;
var isIE6 = Isie && susragent.indexof ("MSIE 6.0")!=-1;
var isIE7 = Isie && susragent.indexof ("MSIE 7.0")!=-1;
var ISFF = Susragent.indexof ("Firefox")!=-1;
var isop = Susragent.indexof ("Opera")!=-1;
var ISSF = Susragent.indexof ("Safari")!=-1 && susragent.indexof ("Chrome") = = 1;
var Isch = Susragent.indexof ("Chrome")!=-1;
var xmlHttp;
function Requestwebservice () {
This is the address of the Web service that we created in the first step
var URL = "Http://localhost:3165/WebSite2/Service.asmx";
In this place we splice
var data;
data = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> ';
data = Data + ' <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 ">";
data = data + ' <soap12:Body> ';
data = Data + ' data = data + ' </soap12:Body> ';
data = data + ' </soap12:Envelope> ';
To create an asynchronous object
XmlHttp = Getxmlhttpobject ();
Xmlhttp.open ("POST", URL, false);
if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype (' Text/xml ');
}
Xmlhttp.setrequestheader ("Content-type", "Application/soap+xml");
Xmlhttp.onreadystatechange = statechanged;
Xmlhttp.send (data);
}
function statechanged () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
Alert (Xmlhttp.getallresponseheaders ());
alert (Xmlhttp.responsetext); This has
var xmldoc = Xmlhttp.responsexml; This one is empty, but the following will let it out.
var xmldoc = Loadxmldoc ();
Xmldoc.setproperty ("SelectionNamespaces", "xmlns:soap=" Http://www.w3.org/2003/05/soap-envelope "xmlns:ws=" http:/ /tempuri.org/"');
This section is a namespace (contains both display and anonymous) processing methods that must be added!
var node = xmldoc.selectsinglenode ("/soap:envelope/soap:body/ws:helloworldresponse/ws:helloworldresult"); This side can have a value on the OK, in order to it before and after the consumption of 1 weeks time!
document.getElementById ("Div1"). InnerHTML = Node.nodetypedvalue;
}
}
}
function Getxmlhttpobject () {
var xmlHttp = null;
try {
Firefox, Opera 8.0+, Safari
XmlHttp = new XMLHttpRequest ();
}
catch (e) {
Internet Explorer
try {
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (e) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function Loadxmldoc () {
var xmldoc;
if (Isie) {
xmldoc = Getmsxmlparser ();
Xmldoc.async = false;
Xmldoc.loadxml (Xmlhttp.responsetext); WebService response need to use Loadxml
Xmldoc.load (Xmlhttp.responsetext); Load is required for loading XML documents
}
else {
xmldoc = Xmlhttp.responsexml;
if (!xmldoc) {
xmldoc = (new Domparser ()). Parsefromstring (Xmlhttp.responsetext, ' text/xml ');
}
}
return xmldoc;
}
function Getmsxmlparser () {
var parser = [' msxml2.domdocument.6.0 ',
' msxml2.domdocument.5.0 ',
' msxml2.domdocument.4.0 ',
' msxml2.domdocument.3.0 ',
' MSXML2. DOMDocument ',
' Microsoft.XMLDOM ']; The same as MSXML. DOMDocument
for (var i in parser) {
try {
var xparser = new ActiveXObject (Parser[i]);
if (Xparser) {
return xparser;
}
}
catch (e) {}
}
return null;
}
</script>
</body>

======== Background cs File ========
In fact, there is no substance in this paragraph
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
}

========webservice Code ========
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
[System.Web.Script.Services.ScriptService]
public class Service:System.Web.Services.WebService
{
Public Service () {
If you are using a design component, uncomment it to follow the line
InitializeComponent ();
}
[WebMethod]
public string HelloWorld () {
Return to "Hello World";
}
}

responsetext======== returned by ========
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<soap:envelope xmlns:soap= "Http://www.w3.org/2003/05/soap-envelope"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >
<soap:Body>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>

End!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.