$(function(){
//获取网页中所有的form表单
$("form").each(function(){
//注册表单的提交事件
$(this).submit(function(event) {
//屏蔽表单的注册
event.preventDefault();
//获取url
var url = $(this).attr("action");
request(url,
‘POST‘,
JSON.stringify($(this).serializeObject()),
function(){alert(‘123‘);},
null);
});
});
});
//将$.ajax函数 转化成一个简单的接口
function request(url, method, param, callback, fail){
//alert(param);
$.ajax({
type: method,
//async:false,
contentType: "application/json;charset=UTF-8",
dataType: "json",
url: url,
data: param,
success: callback,
error: fail,
});
}
//将form表单里面的数据转化为json对象
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || ‘‘);
} else {
o[this.name] = this.value || ‘‘;
}
});
return o;
};
//再用JSON.stringify()函数将json对象转化为json字符串,就转化出去了