在js中声明了一个对象var 0bj={name:'admin'}怎么将它以ajax方式传到后台,以“{name:'admin'}”的对象打
发布网友
发布时间:2022-05-05 09:32
我来回答
共3个回答
热心网友
时间:2023-10-03 08:27
function toJSON(txtOrObj,hasIndent){
var data=txtOrObj;
if(typeof data=='string') try{data=eval('('+data+')')}catch(e){return ""};
var draw=[],last=false,isLast=true,indent=0;
function notify(name,value,isLast,formObj){
if(value&&value.constructor==Array){
draw.push((formObj?('"'+name+'":'):'')+'[');
for (var i=0;i<value.length;i++) notify(i,value[i],i==value.length-1,false);
draw.push(']'+(isLast?'':(',')));
}else if(value&&typeof value=='object'){
draw.push((formObj?('"'+name+'":'):'')+'{');
var len=0,i=0;
for(var key in value)len++;
for(var key in value)notify(key,value[key],++i==len,true);
draw.push('}'+(isLast?'':(',')));
}else{
if(typeof value=='string')value='"'+value+'"';
draw.push((formObj?('"'+name+'":'):'')+value+(isLast?'':','));
};
};
你的调用
function testJson(){
var url="EmpServlet";
var data={name:"1212",username:"0",sex:["F","M"]};
$.post(url,{str:toJSON(data)},function(data){
});
}
参考资料:http://apps.hi.baidu.com/share/detail/7559691
热心网友
时间:2023-10-03 08:28
var obj="{name:'admin'}";
热心网友
时间:2023-10-03 08:28
$.post(rul,date,function(data){
});