Html response content type xml

XML on the Server

XML can easily be stored and generated by a standard web server.

Storing XML Files on the Server

XML files can be stored on an Internet server exactly the same way as HTML files.

Start Windows Notepad and write the following lines:

Save the file on your web server with a proper name like «note.xml».

Generating XML with PHP

XML can be generated on a server without any installed XML software.

To generate an XML response from the server using PHP, use following code:

Note that the content type of the response header must be set to «text/xml».

If you want to study PHP, you will find our PHP tutorial on our homepage.

Generating XML with ASP

To generate an XML response from the server — simply write the following code and save it as an ASP file on the web server:

Note that the content type of the response must be set to «text/xml».

If you want to study ASP, you will find our ASP tutorial on our homepage.

Generating XML From a Database

XML can be generated from a database without any installed XML software.

To generate an XML database response from the server, simply write the following code and save it as an ASP file on the web server:

<%
response.ContentType = «text/xml»
set conn=Server.CreateObject(«ADODB.Connection»)
conn.provider=»Microsoft.Jet.OLEDB.4.0;»
conn.open server.mappath(«/datafolder/database.mdb»)

sql=»select fname,lname from tblGuestBook»
set rs=Conn.Execute(sql)

The example above uses ASP with ADO.

If you want to study ASP and ADO, you will find the tutorials on our homepage.

Читайте также:  Python unpack list in list comprehension

Transforming XML with XSLT on the Server

This ASP transforms an XML file to XHTML on the server:

<%
‘Load XML
set xml = Server.CreateObject(«Microsoft.XMLDOM»)
xml.async = false
xml.load(Server.MapPath(«simple.xml»))

‘Load XSL
set xsl = Server.CreateObject(«Microsoft.XMLDOM»)
xsl.async = false
xsl.load(Server.MapPath(«simple.xsl»))

‘Transform file
Response.Write(xml.transformNode(xsl))
%>

  • The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory.
  • The second block of code creates another instance of the parser and loads the XSL file into memory.
  • The last line of code transforms the XML document using the XSL document, and sends the result as XHTML to your browser. Nice!

Источник

XMLHttpRequest: responseType property

The XMLHttpRequest property responseType is an enumerated string value specifying the type of data contained in the response.

It also lets the author change the response type. If an empty string is set as the value of responseType , the default value of text is used.

Value

A string which specifies what type of data the response contains. It can take the following values:

An empty responseType string is the same as «text» , the default type.

The response is a JavaScript ArrayBuffer containing binary data.

The response is a Blob object containing the binary data.

The response is an HTML Document or XML XMLDocument , as appropriate based on the MIME type of the received data. See HTML in XMLHttpRequest to learn more about using XHR to fetch HTML content.

The response is a JavaScript object created by parsing the contents of received data as JSON.

The response is a text in a string.

Note: When setting responseType to a particular value, the author should make sure that the server is actually sending a response compatible with that format. If the server returns data that is not compatible with the responseType that was set, the value of response will be null .

Exceptions

An attempt was made to change the value of responseType on an XMLHttpRequest which is in synchronous mode but not in a Worker . For additional details, see Synchronous XHR restrictions below.

Читайте также:  Python консоль в приложении

Usage notes

Synchronous XHR restrictions

You cannot change the value of responseType in a synchronous XMLHttpRequest except when the request belongs to a Worker . This restriction is designed in part to help ensure that synchronous operations aren’t used for large transactions that block the browser’s main thread, thereby bogging down the user experience.

XHR requests are asynchronous by default; they are only placed in synchronous mode by passing false as the value of the optional async parameter when calling open() .

Restrictions in Workers

Attempts to set the value of responseType to document are ignored in a Worker .

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Apr 8, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Web API Request/Response Data Formats

Here, you will learn how Web API handles different formats of request and response data.

Media Type

Media type (aka MIME type) specifies the format of the data as type/subtype e.g. text/html, text/xml, application/json, image/jpeg etc.

In HTTP request, MIME type is specified in the request header using Accept and Content-Type attribute. The Accept header attribute specifies the format of response data which the client expects and the Content-Type header attribute specifies the format of the data in the request body so that receiver can parse it into appropriate format.

For example, if a client wants response data in JSON format then it will send following GET HTTP request with Accept header to the Web API.

GET http://localhost:60464/api/student HTTP/1.1 User-Agent: Fiddler Host: localhost:1234 Accept: application/json 

The same way, if a client includes JSON data in the request body to send it to the receiver then it will send following POST HTTP request with Content-Type header with JSON data in the body.

Читайте также:  Как сделать пунктирную или прямую линию?

POST http://localhost:60464/api/student?age=15 HTTP/1.1 User-Agent: Fiddler Host: localhost:60464 Content-Type: application/json Content-Length: 13

Web API converts request data into CLR object and also serialize CLR object into response data based on Accept and Content-Type headers. Web API includes built-in support for JSON, XML, BSON, and form-urlencoded data. It means it automatically converts request/response data into these formats OOB (out-of the box).

public class Student < public int Id < get; set; >public string Name < get; set; >> public class StudentController : ApiController < public Student Post(Student student) < // save student into db var insertedStudent = SaveStudent(student); return insertedStudent; > > 

As you can see above, the Post() action method accepts Student type parameter, saves that student into DB and returns inserted student with generated id. The above Web API handles HTTP POST request with JSON or XML data and parses it to a Student object based on Content-Type header value and the same way it converts insertedStudent object into JSON or XML based on Accept header value.

The following figure illustrates HTTP POST request in fiddler.

In the above figure, Accept header specifies that it expects response data in XML format and Content-Type specifies that the student data into request body is in the JSON format. The following is the response upon execution of the above request.

The same way, you can specify different request & response format using accept and content-type headers and Web API will handle them without any additional changes.

The following HTTP POST request sends data in XML format and receives data in JSON format.

The above HTTP POST request will get the following response upon execution.

Thus, Web API handles JSON and XML data by default. Learn how Web API formats request/response data using formatters in the next section.

Источник

Оцените статью