XML Error

  • Hello,

    My question has nothing to do with sqlsever, but since there are so many bright people on this forum, I figure it is worth a shot.

    I'm trying to learn about XML, SOAP and getting an error on one of my first scripts.

    The line that seems to be the culprit (full code below) .... xhHTTP.send(xdDoc);

    Line: 50

    Error: System error: -1072896659

    In debug mode the error is:

    msxml3.dll: System error: -1072896659

    Any suggestions would be much appreciated!  Thanks.

    ***My Code ***

    <html>

    <head>

    <title>GET Leads</title>

    <script language="JavaScript">

    function updateTotal() {

     var unitPrice = orderForm.unitPrice.value;

     var qty  = orderForm.qty.value;

     xdDoc =  new ActiveXObject("MSXML.DOMDocument");

     xhHTTP = new ActiveXObject("MSXML2.XMLHTTP");

     xhHTTP.open("GET","http://localhost/updateTotal2.asp?quantity="+qty+"&unitPrice="+unitPrice, false);

     xhHTTP.send();

     xdDoc=xhHTTP.responseXML;

     alert(xdDoc.xml);

     var discount, extPrice;

     discount=xdDoc.selectSingleNode("//o:discount").text + "% off";

     extPrice=xdDoc.selectSingleNode("//o:extPrice").text;

     document.getElementById("discount").innerHTML=discount;

     orderForm.extPrice.value=extPrice;

    }

    function sendOrder() {

     var xdDoc, xhHTTP, sXML

     sXML="<env:Envelope xmlns:env="

     sXML=sXML + "'http://www.w3.org/2003/05/soap-envelope'>"

     sXML=sXML + "<env:Body>"

     sXML=sXML + "<o:addToCart "+

      "xmlns:o='http://www.sernaferna.com/soap/ordersystem'>";

     sXML=sXML + "<o:cartId>"+orderForm.cartId.value+"</o:cartId>";

     sXML=sXML + "<o:item itemId='"+orderForm.itemId.value+"'>";

     sXML=sXML + "<o:quantity>"+orderForm.qty.value+"</o:quanity>"

     sXML=sXML + "<o:extPrice>"+orderForm.extPrice.value+"</o:extPrice>";

     sXML=sXML + "</O:item>";

     sXML=sXML + "</o:addToCart>";

     sXML=sXML + "</env:Body>";

     sXML=sXML + "</env:Envelope>";

     alert(sXML)

     xdDoc = new ActiveXObject("MSXML.DOMDocument");

     xdDoc.loadXML(sXML);

     xhHTTP = new ActiveXObject("MSXML2.XMLHTTP");

     xhHTTP.open("POST","http://localhost/submitOrder1.asp",false);

    xhHTTP.send(xdDoc);

     xdDoc = xhHTTP.responseXML;

     

     var responseName;

     responseName = xdDoc.selectSingleNode("//env:Body").firstChild.nodeName;

     if (responseName == "env:Fault") {

      var reason, message

      reason = xdDoc.selectSingleNode("//env:Reason/env:Text").firstChild.nodeValue;

      message=xDoc.selectSingleNode("//o:message").firstChild.nodeValue;

      alert(reason+":\n"+message);

     } else {

      var orderNumber, status, total;

      cartId = xdDoc.selectSingleNode("//o:cartId").firstChild.nodeValue;

      status = xdDoc.selectSingleNode("//o:status").firstChild.nodeValue;

      itemId = xdDoc.selectSingleNode("//o:itemId").firstChild.nodeValue;

      submitDiv.innerHTML="<b>Item "+itemId+" added to cart #"+cartId+"</b>";

     } 

      

    }

    </script>

    </head>

    <body>

    <h1>SOAP Pricing Tool</h1>

    <form name="orderForm" id="orderForm">

    <input type="hidden" name="itemId" id="itemId" value="ZIBKA" />

    <input type="hidden" name="cartId" id="cartId" value="THX1138" />

    Quantity: <input id="qty" name="qty" size="3" onChange="updateTotal();" value="0" />

    <br />

    Item: <i>Xibka Smiles</i>, by The Polka Dot Zither Band

    <br />

    Discount:  <span id="discount"></span>

    <br />

    Unit Price: <input id="unitPrice" name="unitPrice" value="12.95" size="4" />

    <br />

    Extended Price: <input id="extPrice" name="extPrice" value="0" size="4" />

    <div id="submitDiv">

    <input type="button" value="Add to Cart" id="submitButton" name="submitButton" onclick="sendOrder()">

    </div>

    </form>

    </body>

    </html>

     

  • Try this link to find your answer

    http://p2p.wrox.com/topic.asp?TOPIC_ID=9172 

     

     

     

     


    Kindest Regards,

    Amit Lohia

  • Thanks for the response.

    I had already seen this posting (and many others).  Unfortunately, the link provided is no longer valid (http://support.microsoft.com/default.aspx?scid=gp;%5Bln%5D;gsserr404&style=error).

    I did download the the dlls from Microsoft though I appeared to have the latest anyway.

    The posting never gives a definitive resolution.

  • Kim -- this is my third attempt to post an answer in this thread. Excuse me for being brief this time <g>.

    The individuals on the thread were telling you to check the wrox thread suggestions, not the msdn embedded link in the thread, to answer your question, so it does not matter that the msdn link was not live.  However, the suggestions would not work for you, because your code is riddled with errors.

    1. If instead of doing alert(sXML) you did alert(xdDoc.xml) you would find out you hadn't loaded your sXML into the DOM because it was not well formed.  Please do this:

    if (! xdDoc.loadXML(sXML)) alert(xdDoc.parseError.reason) ;

    ... to resolve the two mis-spellings that I saw.  And this is for production code, not just for the example. ALways test that your constructed request loaded correctly (although naturally in production you don't handle it with an alert!).

    2. you can't send the object xdDoc.  You have to send the string contents of the message. xhHTTP.send(sXML) would do as well as xhHTTP.send(xdDoc.xml) -- except that, see above, it's a good idea to validate your post against the DOM before sending.

    3.  When you receive the response, you can't load the objject xhHTTP into xdDoc.  You have to load the string contents of the response back into the DOM object.  And use xhHTTP.loadXML(xdDoc.responseText), not the .responseXML.  At this point you don't even know if you got well-formed XML back.  What if the server has responded with an HTML error page for some reason, such as you reached the wrong page?

    4. Before doing #3, you should be testing the statusCode and status values of the xhHTTP object, because you don't know if the post was successful.  What if the request timed out, or was not properly authorized?

    5. You should consider using the alternative MSXML2.ServerXMLHTTP object even though you are posting on the client.  It is later-built and better-behaved. It does not provide for asynchronous posts but you don't need that.

    6. You may have to think about adding some content headers to the post, such as:

    xhHTTP.setRequestHeader("Content-Type", "text/xml") ;

    xhHTTP.setRequestHeader("SOAPAction", "XXX") ;

    ... this is dependent on the server-side SOAP engine, so you won't know until you experiment.

    Hope this gets through this time, and hope it helps,

    >L<

  • Lisa,

    Thank you for taking the time to post a response to my question.  You definately got me on the right path.  I did uncover several errors as I'm obviously nowhere near as knowledgeable/experienced as you are.  As I said originally, this is my first try with web services, SOAP, and XML.  I was trying the example set out in the wrox book, Beginning XML (p.585).

    If anyone wants to see what I came up with.

    --test.html--

    <html>

    <head>

    <title>GET Tester</title>

    <script language="JavaScript">

    function updateTotal() {

     var unitPrice = orderForm.unitPrice.value;

     var qty  = orderForm.qty.value;

     xdDoc =  new ActiveXObject("MSXML.DOMDocument");

     xhHTTP = new ActiveXObject("MSXML2.XMLHTTP");

     xhHTTP.open("GET","http://localhost/soap/updateTotal2.asp?quantity="+qty+"&unitPrice="+unitPrice, false);

     xhHTTP.send();

     xdDoc=xhHTTP.responseXML;

     var discount, extPrice;

     discount=xdDoc.selectSingleNode("//o:discount").text + "% off";

     extPrice=xdDoc.selectSingleNode("//o:extPrice").text;

     document.getElementById("discount").innerHTML=discount;

     orderForm.extPrice.value=extPrice;

    }

    function sendOrder() {

      var xdDoc, xhHTTP, sXML,sXML2

      sXML="<env:Envelope xmlns:env="

      sXML=sXML + "'http://www.w3.org/2003/05/soap-envelope'>"

      sXML=sXML + "<env:Body>"

      sXML=sXML + "<o:addToCart "+

        "xmlns:o='http://www.sernaferna.com/soap/ordersystem'>";

      sXML=sXML + "<o:cartId>"+orderForm.cartId.value+"</o:cartId>";

      sXML=sXML + "<o:item itemId='"+orderForm.itemId.value+"'>";

      sXML=sXML + "<o:quantity>"+orderForm.qty.value+"</o:quantity>"

      sXML=sXML + "<o:extPrice>"+orderForm.extPrice.value+"</o:extPrice>";

      sXML=sXML + "</o:item>";

      sXML=sXML + "</o:addToCart>";

      sXML=sXML + "</env:Body>";

      sXML=sXML + "</env:Envelope>";

      xdDoc = new ActiveXObject("MSXML.DOMDocument");

      xdDoc.loadXML(sXML);

      xdDoc2 = new ActiveXObject("MSXML.DOMDocument");

      xhHTTP = new ActiveXObject("MSXML2.XMLHTTP");

      xhHTTP.open("POST","http://localhost/soap/addtocart.asp",false);

      xhHTTP.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');

      xhHTTP.setRequestHeader('SOAPAction', 'http://localhost/soap');

      xhHTTP.setRequestHeader("Cache-Control","no-cache");

      if (! xdDoc.loadXML(sXML)) alert(xdDoc.parseError.reason) ;

      // alert(xdDoc.xml)

       xhHTTP.send(xdDoc.xml);

      sXML2=xhHTTP.responseText;

      if (! xdDoc2.loadXML(sXML2)) alert(xdDoc2.parseError.reason);

      var responseName;

      responseName = xdDoc2.selectSingleNode("//env:Body").firstChild.nodeName;

      if (responseName == "env:Fault") {

        var reason, message

         reason = xdDoc.selectSingleNode("//env:Reason/env:Text").firstChild.nodeValue;

         message=xDoc.selectSingleNode("//o:message").firstChild.nodeValue;

         alert(reason+":\n"+message);

       } else {

         var orderNumber, status, total;

         cartId = xdDoc2.selectSingleNode("//o:cartId").firstChild.nodeValue;

         status = xdDoc2.selectSingleNode("//o:status").firstChild.nodeValue;

         itemId = xdDoc2.selectSingleNode("//o:itemId").firstChild.nodeValue;

         submitDiv.innerHTML="<b>Item "+itemId+" added to cart #"+cartId+"</b>";

      }

     

      }

    </script>

    </head>

    <body>

    <h1>SOAP Pricing Tool</h1>

    <form name="orderForm" id="orderForm">

    <input type="hidden" name="itemId" id="itemId" value="ZIBKA" />

    <input type="hidden" name="cartId" id="cartId" value="THX1138" />

    Quantity: <input id="qty" name="qty" size="3" onChange="updateTotal();" value="0" />

    <br />

    Item: <i>Xibka Smiles</i>, by The Polka Dot Zither Band

    <br />

    Discount:  <span id="discount"></span>

    <br />

    Unit Price: <input id="unitPrice" name="unitPrice" value="12.95" size="4" />

    <br />

    Extended Price: <input id="extPrice" name="extPrice" value="0" size="4" />

    <div id="submitDiv">

    <input type="button" value="Add to Cart" id="submitButton" name="submitButton" onclick="sendOrder()">

    </div>

    </form>

    </body>

    </html>

    --addToCart.asp

    <%@ Language="VBScript" %>

    <%

    Response.ContentType="text/xml"

    Dim xdRequestXML

    Dim sCartid, sItemid, sQuantity, sProcedure

    Dim sStatus

    set xdRequestXML = Server.CreateObject("MSXML.DOMDocument")

    xdRequestXML.load Request

    sCartid = xdRequestXML.selectSingleNode("//o:cartId").firstChild.nodeValue

    sItemid = xdRequestXML.selectSingleNode("//o:item/@itemId").nodeValue

    sQuantity = xdRequestXML.selectSingleNode("//o:quantity").firstChild.nodeValue

    sProcedure = xdRequestXML.documentElement.firstChild.firstChild.nodeName

    On Error Resume Next

    'In the production system we'd add the item to the cart here

    If Err.number<> 0 Then

     Response.Write ErrorXML("Sender", "rpc:BadArguments","Improper arguments","1000","Unkown cart or item")

    ElseIf sProcedure <> "o:addToCart" Then

     Response.Write ErrorXML("Sender", "rpcrocedureNotPresent","Procedure not supported","0000","Unkown procedure")

    Else

     Response.Write SuccessXML(sCartid,sItemid,sQuantity)

    End If

    Function ErrorXML(sFaultCode, sSubvalue, sReason, sErrorCode, sMessage)

    Dim sXML

    sXML="<env:Envelope xmlns:env="

    sXML=sXML & "'http://schemas.xmlsoap.org/soap/envelope/'>"

    sXML=sXML & vbNewLine & "<env:Body>"

    sXML=sXML & "<env:Fault>"&vbNewLine

    sXML=sXML & "<env:Code>"&vbNewLine

    sXML=sXML & "<env:Value>env:"&sFaultCode& "</env:Value>" & vbNewLine

    sXML=sXML & "<env:Subcode>"&vbNewLine

    sXML=sXML & "<env:Value>"&sSubvalue&"</env:Value>" & vbNewLine

    sXML=sXML & "</env:Subcode>"&vbNewLine

    sXML=sXML & "<env:Code>"&vbNewLine

    sXML=sXML & "<env:Text>"&sReason& "</env:Text>" & vbNewLine

    sXML=sXML & "</env:Reason>"&vbNewLine

    sXML=sXML & "<envetail>"&vbNewLine

    sXML=sXML & "<o:orderFaultInfo "

    sXML=sXML & "xmlns:o='http://www.sernaferna.com/soap/ordersystem'>"

    sXML=sXML & vbNewLine

    sXML=sXML & "<o:errorCode"& sErrorCode& "</o:errorCode>" &vbNewLine

    sXML=sXML & "<o:message>"& sMessage& "</o:message>" & vbNewLine

    sXML=sXML & "</o:orderFaultInfo>" & vbNewLine

    sXML=sXML & "</env:Fault>" &vbNewLine

    sXML=sXML & "</env:Body>" &vbNewLine

    sXML=sXML & "</env:Envelope>"&vbNewLine

    ErrorXML=sXML

    End Function

    Function SuccessXML(sCardid,sItemid,sQuantity)

    Dim sXML

    sXML="<env:Envelope xmlns:env="

    sXML=sXML & "'http://www.w3.org/2003/05/soap-envelope'>"

    sXML=sXML & vbNewLine & "<env:Body>" & vbNewLine

    sXML=sXML & "<o:addToCartResponse "

    sXML=sXML & "xmlns:o='http://www.sernaferna.com/soap/ordersystem'>"

    sXML=sXML & vbNewLine

    sXML=sXML & "<o:cartId>"& sCartid& "</o:cartId>" & vbNewLine

    sXML=sXML & "<o:status>OK</o:status>" & vbNewLine

    sXML=sXML & "<o:quantity>"& sQuantity & "</o:quantity>" &vbNewLine

    sXML=sXML & "<o:itemId>" & sItemid & "</o:itemId>" &vbNewLine

    sXML=sXML & "</o:addToCartResponse>" & vbNewLine

    sXML=sXML & "</env:Body>" & vbNewLine

    sXML=sXML & "</env:Envelope>"

    SuccessXML=sXML

    End Function

    %>

    --updateTotal2.asp--

    <%@ Language="VBScript" %>

    <%

    Response.ContentType="text/xml"

    Dim quantity, unitPrice

    Dim discount, extPrice

    On Error Resume Next

    quantity =Request.QueryString.Item("quantity")

    unitPrice=Request.QueryString.Item("unitPrice")

    discount=QuantityDiscount(quantity)

    extPrice=(CDbl(quantity) * CDbl(unitPrice))*(1-(discount/100))

    If Err.number <> 0 Then

     Response.Write(ErrorXML())

    Else

     Response.Write(SuccessXML(discount, extPrice))

    End If

    Function QuantityDiscount(quantity)

     'for example sake, just return a fixed amount

     QuantityDiscount=10

    End Function

    Function ErrorXML()

     Dim sXML

     sXML="<Error><Reason>Invalid numbers</Reason></Error>"

     ErrorXML=sXML

    End Function

    Function SuccessXML(sDiscount, sExtprice)

    Dim sXML

    sXML="<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'>"

    sXML=sXML & vbNewLine & " <env:Body>"

    sXML=sXML & vbNewLine & " <o:UpdateTotalResponse"

    sXML=sXML & vbNewLine & " xmlns:o='http://localhost/soap/'>"

    sXML=sXML & vbNewLine & " <o:unitPrice>" & unitPrice & "</o:unitPrice>"

    sXML=sXML & vbNewLine & " <o:quantity>" & quantity & "</o:quantity>"

    sXML=sXML & vbNewLine & " <o:discount>" & discount & "</o:discount>"

    sXML=sXML & vbNewLine & " <o:extPrice>" & extPrice & "</o:extPrice>"

    sXML=sXML & vbNewLine & " </o:UpdateTotalResponse>"

    sXML=sXML & vbNewLine & " </env:Body>"

    sXML=sXML & vbNewLine & " </env:Envelope>"

    successXML = sXML

    End Function

    %>

  • To be honest, Kim, I have no idea whether the mistakes are yours or the book's.  I don't have the book (or any other book on this subject, FWIW!). 

    But if they wrote the code that you showed, rather than your having made these errors as typos when you transferred the example to a running HTML file, I'd throw the book out <g>.

    I thought of one other thing you should deal with up-front (and that any self-respecting example ought to show, no matter how basic the example): is this page actually going to go up on the web on a server, for folks to download and use in a browser?

    If so, you need to look into the different syntax that is required in different browsers to create an HTTPRequest object. new ActiveXObject("MSXML2.XMLHTTP") ain't gonna cut it in FireFox.

    The basic strategy is: test the browser first, then instantiate the object according to the appropriate syntax.  If you look here http://www.w3schools.com/dom/dom_http.asp you will see the requisite code. 

    I do appreciate the fact that you are using a "low-level" approach to sending SOAP messages, which I think is a very good idea in many situations. 

    There are many packaged solutions that hide the details of creating a message and sending it out over HTTP.  "Under the covers", they all use some analog of the basic elements you are learning about right now. 

    IMHO the packaged solutions don't make the process all that much simpler, and they often have client and server elements that are more tightly coupled than they should be.  (Translation: try to access somebody else's web service with your framework-generated client component, and if they didn't use the same framework, there are, um, *issues*.)

    You will be better off for learning exactly what goes into a SOAP request when you start out -- even if you want to "hide the complexity of the process" later.

    Regards,

    >L<

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply