encode
(
Mixed o
)
:
String
Shorthand for
Ext.util.JSON.encode
Parameters:
“Ext.encode”可以解决特殊字符的问题。不过输出的结果为:
o
: MixedThe variable to encode
String
The JSON string
返回的是严格json串;
对于下面的情况也可以应用:
1.
var gradeInit = function() {
var initRecs = gradeInfoSM.getSelections();
if (initRecs.length < 1) {
Ext.Msg.alert('提示', '请选中所需初始化的数据');
return;
}
var personArry = [];
for (var i = 0; i < initRecs.length; i++) {
var record = {};
record.personId = initRecs[i].get('personId');
record.personName = initRecs[i].get('personName');
record.id = initRecs[i].get('id');
record.year = new Date().format("Y");
personArry.push(record);
}
console.info(personArry);
Ext.Msg.confirm("提示", "您确定初始化数据?", function(btn) {
if (btn == "yes") {
Ext.Ajax.request({
url : pkuhrms.ContextRoot
+ '路径',
params : {
personjsonArry : Ext.util.JSON.encode(personArry)
},
success : function(response, options) {
var json = Ext.decode(response.responseText);
if (true == json.success) {
Ext.Msg.alert("提示", json.msg, function() {
queryFn();
});
} else {
Ext.Msg.alert('提示', json.msg, function() {
queryFn();
});
}
},
failure : function(response, options) {
Ext.Msg.hide();
Ext.Msg.alert("提示信息", "查询失败,请重试");
}
})
}
})
}
对于上面的程序片段“personjsonArry : Ext.util.JSON.encode(personArry)”:后台的接受类型为Object:
<img src="https://img-blog.csdn.net/20140910144938072?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenByeWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
所以后台还得专门的方法来解析
2.
saveJson = "{'id':" + personForm.getForm().findField('id').getValue()
+",'remark':"+Ext.encode(personForm.getForm().findField('remark').getValue())+"}";
“Ext.encode”可以解决特殊字符的问题。不过输出的结果为:
{'id':20141006187027,'remark':"kkk"},一个是由双引号括起的,一个则没有;
注意:如果json写成这样:
saveJson = "{'id':'" + personForm.getForm().findField('id').getValue()
+"','remark':'"+Ext.encode(personForm.getForm().findField('remark').getValue())+"'}";
那么对于remark的输出结果:
{'id':'20141006188007','remark':'"kkkkk"'}
数据库每次都会多一个“”;