I"m trying to convert my HTML form data to JSON object for submitting via Ajax. It seems to "work", but i'm not getting the JSON format I want. It probably has to do with the
JSON.stringify($("#frm_login").serializeObject())
The JSON output i am getting is this:
[{"name":"username","value":"mike"},{"name":"password","value":"test123"}]
But the JSON output I REALLY want to get is something simpler, similar to this:
{
"username":"mike",
"password":"test123"
}
Below is my simple html and ajax code:
<form id="frm_login" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="password">Password:</label>
<input type="password" name="password" id="password" value="" />
<input type="button" data-theme="b" name="btn_login" id="btn_login" value="Login">
</form>
And my ajax code:
$(document).on('click', '#btn_login', function(){
var url = 'http://localhost/proto01/api/users/token';
$.ajax({
url: url, dataType: "json", async: true,
type: "POST",
headers: { Accept : "application/json; charset=utf-8", "Content-Type": "application/json; charset=utf-8" },
data: JSON.stringify($("#frm_login").serializeObject()),
success: function (result) {
alert("Success");
},
error: function (request,error) {
alert('Network error has occurred please try again!' + error);
}
});
});