I'm trying to create an HTML page that can submit the form data to the server in the form of JSON. I consulted the answers to this question and am using the following code to do this:
<head>
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.json.org/json2.js"></script>
<script type="text/javascript">
$(function() {
var frm = $(document.myform);
var dat = JSON.stringify(frm.serializeArray());
alert("I am about to POST this:\n\n" + dat);
$.ajax({
type: "POST",
data: dat,
success: function(){},
dataType: "json",
contentType : "application/json"
});
});
</script>
</head>
<body onload="javascript:document.myform.submit()">
<form action="http://www.foo.com/" method="post" name="myform">
<input name="firstName" value="harry" />
<input name="lastName" value="tester" />
<input name="toEmail" value="testtest@test.com" />
</form>
However, if I intercept the request using Burp proxy tool, I can see that for some reason, the Content-Type becomes application/x-www-form-urlencoded
as soon as the request leaves the browser. Here's a screenshot of the request:
I would like to know why is this happening with the request? Why is the browser changing the Content-Type in the request? Is there a better way to do this?
PS: I've tried this without jQuery (using XHR as explained here).