如何从http请求实现函数返回

时间:2017-06-29 03:58:18

标签: 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-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文件中实现该功能。就像将参数传递给该函数一样。那我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您可以在浏览器中调用它,如下所示:

policyFunction = eval("(" + validations.policyFunction + ")");

failures = policyFunction.call({ failedPolicyRequirements: [] },
    fullObject,
    value,
    params,
    propertyName);

如果fullObject是整个对象,则value是特定属性的值,params是验证函数中所需的任何参数,propertyName是属性的名称。