JDelage
JDelage

Reputation: 13672

What setRequestHeader is needed to send a JSON string via Ajax with POST?

All,

I want to send a large-ish JSON string to my server using an Ajax request with POST. I haven't done Ajax+POST before (only GET) so I looked for some info on the web. What confuses me is that some resources say to include those three lines:

request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");

Others only say to use the first (Content-type). So, are those 3 necessary? What do they do?

Upvotes: 3

Views: 5220

Answers (2)

MrCode
MrCode

Reputation: 64526

Content-type

This indicates to the server, the type of data you are sending in the request body (ie POST data). The value in your question application/x-www-form-urlencoded is used for submitting HTML forms, where the input data is sent in key-value form for example:

xmlHttp.send("firstName=bob&lastName=Smith&age=43")

The values should be URL Encoded (aka Percent Encoded) to prevent any special characters breaking the format. If you send this type of data in a POST request without setting the content type, most servers will detect the data type and handle it anyway. It's good practice to set it though (leaves no ambiguity), even though it's not always required.

JSON data can be sent as application/x-www-form-urlencoded, you simply use a key and set the value to the JSON.

myData={"firstName":"bob","age":"43"}

The above JSON should be URL Encoded, in Javascript you can use encodeURIComponent(), I have not encoded it in the example, for the purpose of making it human readable here. This is the most common format for sending POST data and most server side languages will automatically parse the key-value pairs into a native array or construct of some sort.

Alternatively, you can send your JSON literally as the request body, with no key or other data labels. Example:

xmlHttp.send('{"firstName":"bob","age":"43"}');

Using this way, you should set the Content-type to application/json but again, most servers will handle it without that because as far as the HTTP request is concerned, it is just text data. On the server side, you would need to get access to the raw POST data to obtain the JSON. Most server side languages will give you access to the raw post data.

Content-length

As with content-type, it's good practice to set the content-length. It tells the server how much data you are sending and so makes it easier for the server to know when all data is received. That said, it won't usually cause a problem if it's not set, but the W3 docs state that it SHOULD be used.

Connection: close

Sending close for the connection header indicates to the server that you don't support, or don't want to use a persistent connection for the request. The server may respond with a Connection: Keep-alive header, which is not telling the client to keep the connection open, it is just indicating to the client that it supports Persistent Connections. Persistent connections allow the reuse of the same TCP connection for multiple requests, instead of opening and closing for each request. There is no need to set this header in ajax requests. If you don't set it, the browser will decide and will set it itself.

Summary

To summarize, Connection is not needed, Content-length should be used. Content-type should be used but depending on the server and the server side code, it's probably not needed.

Upvotes: 4

alevin
alevin

Reputation: 1

You do it when you "open" the request object:

request.open("POST", <url>);

Upvotes: 0

Related Questions