Asynchronous JavaScript and XML data formats: XML SOAP HTML

Source: Internet
Author: User
Tags format soap require return web services client




Article Introduction: Ajax and Web service data formats: XML SOAP HTML.





When Ajax was created, it was meant to be: Asynchronous JavaScript and XML, asynchronous JavaScript and XML, which is generally the case:


    • First create a network service, such as a PHP page that can pass HTTP get/post parameters, and then return an XML-formatted response
    • Write some of the client's JS code. such as passing parameters and parsing XML. These calls are asynchronous, so the browser is not stuck in the process of waiting for data.
    • Work with the data in the XML, and then update the DOM node


The term Ajax is abused by developers and sales people. Today's word, which is supposed to be all caps, has become a new word, indicating the transfer of data without refreshing the page. The reasons are:


    • There is no need to use asynchronous methods
    • JavaScript is not necessarily used
    • Nor does it require XML


Ultimately, you have to transfer data between two devices regardless of the technology you use. This is the first part of the Rockux serial data format, and we'll talk about more data and features later.



Xml



In the beginning, XML is the best choice. Many languages have functions that support XML format parsing and serialization. Even if the language you're using doesn't support XML, he's actually a piece of text.


<?xml
							version="1.0"?>
<products>
<book>
<title>The Principles of Beautiful Web Design, 2nd Edition</title>
<url>http://www.sitepoint.com/books/design2/</url>
<author>Jason Beaird</author>
<publisher>SitePoint</publisher>
<price
							currency="USD">39.95</price>
</book>
<book>
<title>jQuery: Novice to Ninja</title>
<url>http://www.sitepoint.com/books/jquery1/</url>
<author>Earle Castledine & Craig Sharkie</author>
<publisher>SitePoint</publisher>
<price
							currency="USD">29.95</price>
</book>
<book>
<title>Build Your Own Database Driven Website</title>
<url>http://www.sitepoint.com/books/phpmysql4/</url>
<author>Kevin Yank</author>
<publisher>SitePoint</publisher>
<price
							currency="USD">39.95</price>
</book>
</products>


The benefits of XML are:


    • XML is readable and easy to understand.
    • Many languages have a good support for XML, and the key is JavaScript.
    • XML is more secure. Data must be extracted and parsed, so malicious data is effectively contained


However, XML also has its drawbacks:


    • There will never be an industry-approved format for XML. You need to adapt RSS and so on, the client's JavaScript must understand the only line
    • The data in XML format may be larger than the amount of data, and in asynchronous interaction, the smaller the data, the better.
    • There are some ambiguities in XML, should a data be a new element or a metadata attribute? As a property may be a small file, but this is not convenient to fit.
    • Parsing XML in JAVASCRITP is very difficult, XPath support is better, so you need to first convert
// grab value in first <data> element
var xml = xhr.responseXML;
var nodes = xml.getElementsByTagName("data");
var data =
						(nodes.length
						>
						0
						? nodes[0].firstChild.nodeValue
						:
						null);
 
 


Many developers think XML is dead, but I don't agree that XML may not be the best choice for Ajax right now, but you don't know how all Web services are deployed. And the popularity of XML is a great thing, don't ignore it.



Soap



SOAP is a standard format for data interchange. The detailed technical details document has hundreds of pages, and overall, SOAP relies on well-defined XML schemas.
Few developers in the world use SOAP (see enough). The beauty of soap is that the client's library can automatically convert the XML into native objects. Like what. NET developers can create a SOAP-based network service that clients do not need to do much. As developers, most of them use C #, and they don't care what remote machines use.
But the disadvantage of soap:


    • Soap even transcends the lengthy XML
    • It is still difficult to handle soap in JavaScript. Although SOAP libraries can assist with processing, there is still a lot of pressure on developers and browsers


Soap is still the choice between service-side communication, especially in the case of the same LAN. But not much in Ajax.



Html



If you want to insert code into a page via an AJAX request, HTML is a simple format that does not require much processing. For example, suppose you need to display a shopping cart on every page. You already have such HTML code on the server side that you can return the same HTML code after you have purchased the opinion item.
Its benefits include:


    • Easy to use existing code to create a network service
    • No need for complex data processing on the client
    • HTML code can be quickly inserted into the page by innerHTML


But there are also disadvantages:


    • It's hard to isolate real valuable data. For example, if you want to display your cart in a different way elsewhere, you won't be able to use it.
    • The amount of data transferred is also larger, larger than XML
    • Inserting HTML has the risk of damaging existing pages
    • Security may be problematic, the corresponding data may have malicious code


In the following article in Rockux we will talk about:


    • Data formats for AJAX and Web services: JSON and JSONP
    • Data formats for AJAX and Web services: custom return format


Reprint Please specify:
Author: rockux–web Front end
From: Ajax and Web Service data formats: XML SOAP HTML




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.