You could try using the $.ajax
method:
$.ajax({
url: '/some_server_side_script',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
username: $('#username').val(),
password: $('#password').val(),
}),
success: function(result) {
alert('success');
}
});
In this example I have explicitly specified the Content-Type
HTTP request header to application/json
so that the server knows the exact content type being sent. I have also used the native JSON.stringify
javascript function to convert the javascript object to a JSON string that will be sent to the server.
So assuming that you had #username
and #password
input fields in your DOM the following request payload will be sent to the server:
POST /some_server_side_script HTTP/1.1
Content-Type: application/json
Content-Length: 39
Connection: close
{"username":"john","password":"secret"}
solved How do you put a User Name/Password in JSON format? [closed]