Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript[对象元素]_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript[对象元素]

Javascript[对象元素],javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在尝试从webservice获取一些xml数据。我可以访问所有数据,但无法逐个获取。 我想将数据写入id为“code”的div。它写入[对象元素]。我该怎么办 <script type="text/javascript"> $(document).ready(function () { var returnValue = localStorage.getItem("returnValue"); var userNam

我正在尝试从webservice获取一些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>

$(文档).ready(函数(){
var returnValue=localStorage.getItem(“returnValue”);
var userName=localStorage.getItem(“用户名”);
警报(“你好”+用户名);
var wsUrl=”http://xxx=GetStocks";
var soapRequest=
'\
\
\
\
“+returnValue+”\
\
\
';
$.ajax({
类型:“POST”,
url:wsUrl,
contentType:“text/xml”,
数据类型:“xml”,
数据:soapRequest,
成功:成功,,
错误:processError
});
});
函数processSuccess(数据、状态、请求){
如果(状态=“成功”){
var股票=$(请求响应xml).find(“pStockInfo”);
变量代码=$(req.responseXML).find(“代码”);
var StockScont=股票长度;
$(“#代码”).text(代码[0])//这里我想要第一个代码。
}
}
功能处理错误(数据、状态、请求){
警报(请求响应文本+“”+状态);
}

Xml数据

我想获得您可以使用的第一个代码“FO19428”

你可以用


code[0]
包含
FO19428
。您可以使用jQuery的
.text()
函数获取其内容:

var firstCode = $(codes[0]).text();
这里有一把小提琴:

var responseXML=“
FO19428
ITACA-KC2”; 变量代码=$(responseXML).find(“代码”); var firstCode=$(代码[0]).text(); $(“#代码”).text(第一个代码)
代码[0]
包含
FO19428
。您可以使用jQuery的
.text()
函数获取其内容:

var firstCode = $(codes[0]).text();
这里有一把小提琴:

var responseXML=“
FO19428
ITACA-KC2”; 变量代码=$(responseXML).find(“代码”); var firstCode=$(代码[0]).text(); $(“#代码”).text(第一个代码)
jquery
find
返回找到的所有元素。因此必须
将所有元素映射到它们的
文本内容

试验
test('jquery对XML进行操作',()=>{
const xml=new DOMParser().parseFromString(`
烹饪
饮酒
`“文本/xml”);
let books=$(xml).find('book title').map(函数(){
返回此.textContent;
}).toArray();
expect(xml instanceof Document).toBe(true);
除了(书)之外,还有toEqual([‘烹饪’、‘饮用’);
});
演示
var xml=new DOMParser().parseFromString([
'',
'',
“烹饪”,
'',
'',
“喝酒”,
'',
''
].join(“”),“text/xml”);
var books=$(xml).find('book title').map(函数(){
返回此.textContent;
}).toArray();
控制台。日志(书籍);

jquery
find
返回找到的所有元素。因此必须
将所有元素映射到它们的
textContent

试验
test('jquery对XML进行操作',()=>{
const xml=new DOMParser().parseFromString(`
烹饪
饮酒
`“文本/xml”);
let books=$(xml).find('book title').map(函数(){
返回此.textContent;
}).toArray();
expect(xml instanceof Document).toBe(true);
除了(书)之外,还有toEqual([‘烹饪’、‘饮用’);
});
演示
var xml=new DOMParser().parseFromString([
'',
'',
“烹饪”,
'',
'',
“喝酒”,
'',
''
].join(“”),“text/xml”);
var books=$(xml).find('book title').map(函数(){
返回此.textContent;
}).toArray();
控制台。日志(书籍);

请插入
控制台.log(代码)在<代码>$(“#代码”).文本(代码[0])
并在问题中发布输出。如果问题中包含XML数据,可能在变量内部的一个孤立片段示例中,仅包含处理它的代码。除了在我的浏览器中阻止图像之外,我无法将粘贴数据从图像复制到片段、小提琴等。。要了解为什么/什么是有效的,请插入
console.log(代码)在<代码>$(“#代码”).文本(代码[0])
并在您的问题中发布输出。如果您在问题中包含XML数据,可能是在一个变量中的一个单独的代码片段示例中,其中只有专注于处理它的代码,这会有所帮助。除了在我的浏览器中阻止图像之外,我无法将粘贴数据从图像复制到片段、小提琴等。。看看为什么/什么确实有效。
test('jquery operate on XML', () => {
        const xml = new DOMParser().parseFromString(`
            <bookstore>
                <book>
                    <title>cooking</title>
                </book>
                <book>
                    <title>drinking</title>
                </book>
            </bookstore>
        `, 'text/xml');
    
        let books = $(xml).find('book title').map(function () {
            return this.textContent;
        }).toArray();
        expect(xml instanceof Document).toBe(true);
        expect(books).toEqual(['cooking', 'drinking']);
    });