JavaScript [对象元素]

标签 javascript jquery ajax

我正在尝试从 Web 服务获取一些 xml 数据。我可以访问所有数据,但无法一一获取。 我想将数据写入具有 id“code”的 div。它写的是[对象元素]。我必须做什么?

<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数据

enter image description here

我想获得第一个代码“FO19428”

最佳答案

您可以使用jQuery.eq()

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

关于JavaScript [对象元素],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42581337/

相关文章:

php - 高效有效的表单验证

javascript - 使用 ajax 调用从函数返回值

javascript - 如何将表数据的类从 php 传递到 ajax/javascript

javascript - 如何将数组中的整数相乘?

javascript - 如何在vue js中的选择框之间添加和删除项目?

php - 尝试将 base64(图像)文件保存到服务器,它是空的

jquery $.post 在加载回调函数中不起作用?

javascript - 使用 get() 加载页面之前加载图像

javascript - Function.prototype 是唯一没有原型(prototype)属性的函数吗?

javascript - 获取特定的 Google Analytics 目标漏斗阶段