我需要使用 dataFilter 从 validate 插件返回的 json 字符串的开头删除“//”,我在让它工作时遇到了一些问题。我的代码的相关部分是:
$(document).ready(function () {
$.ajaxSetup({
dataFilter: function(data, type){
return type == 'json' ? data.replace(/^(\/{2})?/, '') : data;
}
});
$("#myForm").validate({
rules: {
cgroup :{
required: true,
remote: {
url: compath + "/v.cfc?method=queryRemote&returnformat=json",
type: "post"
}
}
其他规则和消息已被截断。
自从切换到 Coldfusion 10,它现在在 JSON 前面加上一个安全字符串 (//) 作为前两个字符,我需要在响应端修复这个问题。
我不确定我的错误是在 ajaxSetup 中还是在 validate() 或正则表达式本身中使用。
现在控制台中有错误。在查看 Fiddler 中的响应数据时,如果我在较新的服务器上访问 CFC,我会看到预期的 //true 或 //false,如果我发布到未预先挂起 / 的服务器上的 CFC,则为 true 或 false / 人物。
但是我没有看到任何迹象表明 $.ajaxSetup() 正在做任何事情。
答案:jquery.validate() 会破坏 ajaxSetup()。解决方案是将数据过滤器向下移动到远程部分:
cgroup :{
required: true,
remote: {
url: compath + "/v.cfc?method=queryRemote&returnformat=json",
type: "post",
dataFilter: function(data, type){
return type == 'json' ? data.replace(/^(\/{2})?/, '') : data;
}
}
}