Autologin on https with json
Possible Duplicate:
Convert form data to JS object with jQuery
I need to login my users to external website, but there's a problem, this website processes only json-data POST values, ex. {"username":"user","password":"12345"}
<form action="https://external.com/login" method="POST">
<input name="username" value="user" />
<input name="password" value="12345" />
<input type="submit" />
</form>
This above only sends average key=value data, how can I use to POST json-data to external https?
Here is a simple jquery example using jQuery.ajax()
HTML:
<form action="https://external.com/login" method="POST" onsubmit="return postJson(this)">
<input name="username" value="user" />
<input name="password" value="12345" />
<input type="submit" />
</form>
JQ:
function postJson(form) {
var action = $(form).attr('action');
var user = $('[name="username"]', form).val();
var pass = $('[name="password"]', form).val();
console.log('action=' + action + ', user=' + user + ', pass=' + pass);
$.ajax({
url: action,
type: 'POST',
data: JSON.stringify({ username: user, password: pass }),
success: function(data, status) {
// do something after login
console.log('success');
},
error: function() {
console.log('error');
}
});
return false;
}
JSFiddle
链接地址: http://www.djcxy.com/p/46340.html上一篇: javascript将对象转换为字符串
下一篇: 在https上使用json自动登录