本文介绍了如何从ajax响应中读取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想阅读具体的价值。 通过使用以下代码,我得到以下数据 <? xml 版本 = 1.0 编码 = 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:正文 > < New_Data xmlns = http://tempuri.org/ / > < New_DataResult > MH1234 < / New_DataResult > < / soap:正文 > < / soap:Envelope > 但我只想要价值= MH1234 我尝试了什么: < script type = text / javascript> setInterval( function (){ var webserurl = http:// localhost:4650 / WebService1.asmx; var soaprequest = ' \ < soap:envelope xmlns: xsi =http://www.w3.org/2001/XMLSchema-instancemode =holdxmlns:soap =#unknown/> xmlns:xsd =http://www.w3.org/ 2001 / XMLSchema\ xmlns:soap =http://schemas.xmlsoap.org/soap/envelope/> \ < soap:body xmlns:soap =#unknown > \ < new_data xmlns =http://tempuri.org//> \ < / soap:body> \ '; $ .ajax({ type: POST, url:webserurl, contentType: text / XML, dataType: text, data:soaprequest,成功:SuccessOccur, 错误:ErrorOccur }); function SuccessOccur(data,status,req){ if (status = = success) alert(data)} function ErrorOccur(data,status,req){ alert(req.responseText + + status); } }, 3000 ); < / script> 解决方案 .ajax({ type: POST, url:webserurl, contentType: text / XML, dataType: text, data:soaprequest, success:SuccessOccur, error:ErrorOccur }); function SuccessOccur(data,status,req){ if (status = = success) alert(data)} function ErrorOccur(data,status,req){ alert(req.responseText + + status); } }, 3000 ); < / script> 这个jsFiddle可以给你一个想法:使用XPath在JavaScript中解析(基于SOAP)XML - JSFiddle [ ^ ]。 您将能够找到许多其他特殊代码示例。更一般地,您可以使用JavaScript通用XML解析器来传递SOAP。特别是,您可以使用jQuery: jQuery.parseXML()| jQuery API文档 [ ^ ]。 另请参阅:解析和序列化XML - Web开发人员指南| MDN [ ^ ]。 但我认为Kornfeld Eliyahu Peter在回答评论时提出了更好的方法。请改用JSON。如果您使用Ajax,则意味着您可以访问服务器端代码并​​可以正确编写。 -SA i want to read specific value.By using Below code i got following Data<?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> <New_Data xmlns="http://tempuri.org/" /><New_DataResult>MH1234</New_DataResult> </soap:Body></soap:Envelope>but i want only value= MH1234What I have tried:<script type="text/javascript"> setInterval(function () { var webserurl = "http://localhost:4650/WebService1.asmx"; var soaprequest = ' \ <soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" mode="hold" xmlns:soap="#unknown" /> xmlns:xsd="http://www.w3.org/2001/XMLSchema" \ xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \ <soap:body xmlns:soap="#unknown"> \ <new_data xmlns="http://tempuri.org/" /> \ </soap:body> \ '; $.ajax({ type: "POST", url: webserurl, contentType: "text/XML", dataType: "text", data: soaprequest, success: SuccessOccur, error: ErrorOccur }); function SuccessOccur(data, status, req) { if (status == "success") alert(data) } function ErrorOccur(data, status, req) { alert(req.responseText + " " + status); } }, 3000); </script> 解决方案 .ajax({ type: "POST", url: webserurl, contentType: "text/XML", dataType: "text", data: soaprequest, success: SuccessOccur, error: ErrorOccur }); function SuccessOccur(data, status, req) { if (status == "success") alert(data) } function ErrorOccur(data, status, req) { alert(req.responseText + " " + status); } }, 3000); </script>This jsFiddle can give you the idea: Using XPath to parse (SOAP-based) XML in JavaScript — JSFiddle[^].You will be able to find many other ad-hoc code samples. More generally, you can use a JavaScript general-purpose XML parser to pass SOAP. In particular, you can use jQuery: jQuery.parseXML() | jQuery API Documentation[^].See also: Parsing and serializing XML — Web developer guides | MDN[^].But I think Kornfeld Eliyahu Peter suggested much better approach in his comment to the answer. Use JSON instead. If you use Ajax, it means you have access to the server-side code and can write it properly.—SA 这篇关于如何从ajax响应中读取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 18:32