weixin_33694620 2017-03-03 14:23 采纳率: 0%
浏览 22

Javascript [对象元素]

I am trying to get some xml datas from webservice. I can access all data but i cannot get one by one. I want write data to div that has id "code". It writes [object Element]. What i have to do?

<script type="text/javascript">
        $(document).ready(function () {
            var returnValue = localStorage.getItem("returnValue");
            var userName = localStorage.getItem("userName");
            alert("Hello" + userName);
            var wsUrl = "http://xxx=GetStocks";

            var soapRequest =
                '<?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>\
    <GetStocks xmlns="http://sales.xxx.org/">\
      <token>' + returnValue + '</token>\
    </GetStocks>\
  </soap:Body>\
</soap:Envelope>';

            $.ajax({
                type: "POST",
                url: wsUrl,
                contentType: "text/xml",
                dataType: "xml",
                data: soapRequest,
                success: processSuccess,
                error: processError
            });
        });

        function processSuccess(data, status, req) {
            if (status == "success") {
                var stocks = $(req.responseXML).find("pStockInfo");
                var codes = $(req.responseXML).find("Code");
                var stocksCount = stocks.length;

                $("#code").text(codes[0]); //Here i want first code.               
            }
        }

        function processError(data, status, req) {
            alert(req.responseText + " " + status);
        }
    </script>

enter image description here

Xml Data

enter image description here

I want get first code "FO19428"

  • 写回答

3条回答 默认 最新

  • larry*wei 2017-03-03 14:30
    关注

    You can use jQuery.eq()

     var codes = $(req.responseXML).find("Code");
     var firstCode = codes.eq(0).text(); 
    
    评论

报告相同问题?