Javascript 如何实现从http请求返回函数
我有一个ajax调用,如下所示Javascript 如何实现从http请求返回函数,javascript,rest,openidm,Javascript,Rest,Openidm,我有一个ajax调用,如下所示 $.ajax({ datatype:'json', url: 'http://localhost:9090/openidm/policy/managed/user/'+storeUserId, type:'get', xhrFields: { withCredentials: true } , corssDomain:true, headers: { "X-Reque
$.ajax({
datatype:'json',
url: 'http://localhost:9090/openidm/policy/managed/user/'+storeUserId,
type:'get',
xhrFields: {
withCredentials: true
} ,
corssDomain:true,
headers: {
"X-Requested-With":"XMLHttpRequest"
},
success: function (result){
var validations = result.properties[1].policies[1];
console.log(validations.policyFunction);
},
error:function (error){
console.log (error);
}
});
});
上述ajax调用返回policyFunction,如下所示:
function (fullObject, value, params, property) {
var isRequired = _.find(this.failedPolicyRequirements, function (fpr) {
return fpr.policyRequirement === "REQUIRED";
}), isNonEmptyString = (typeof (value) === "string" && value.length), hasMinLength = isNonEmptyString ? (value.length >= params.minLength) : false;
if ((isRequired || isNonEmptyString) && !hasMinLength) {
return [{"policyRequirement":"MIN_LENGTH", "params":{"minLength":params.minLength}}];
}
return [];
}
我想在我的javascript文件中实现这个函数。比如将参数传递给该函数。那么我该如何实现呢 您可以在浏览器中这样调用它:
policyFunction = eval("(" + validations.policyFunction + ")");
failures = policyFunction.call({ failedPolicyRequirements: [] },
fullObject,
value,
params,
propertyName);
其中fullObject是整个对象,value是特定属性的值,params是验证函数中所需的任何参数,propertyName是属性的名称。是typeof validations.policyFunction='function'?您是指此代码链接吗?var validations=result.properties[1]。策略[1];如果我没有错的话,这是文本是正确的。是的,我知道它不会完全是函数类型。它将是一根绳子