jQuery验证-addMethod/remote-json响应中的错误消息 [英] jquery validation - addMethod/remote - error message in json response

查看:147
本文介绍了jQuery验证-addMethod/remote-json响应中的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用远程方法,或者添加一种模仿远程方法的方法,以便能够将输入的数据发送到Web服务并获得带有错误状态和错误消息的json响应. /p>

所以我有这个json响应:

{
    "isError": "true",
    "errorMessage": "The User Name you chose is already in use. Please enter another name."
}

我已经使用远程方法获得了对/错的响应,但是没有额外的数据.

任何帮助将不胜感激.

修改

对此进行了尝试,但是由于变量不是全局变量,因此当然没有用...

$.validator.addMethod("uniqueUserName", function(value, element) {

      $.ajax({
        type: "POST",
         url: "js/username.json",
         contentType: "application/json; charset=utf-8",  
        dataType:"json",
        data: "{'" + $('#enterEmail').attr('id') + "': '" + $('#enterEmail').val() + "'}", 
        dataFilter: function(data) {
            var isError = data.isError;
            var uniqueError = data.errorMessage;
            if(isError == "true"){
                return false;
            }
        }

     })

}, uniqueError); //this last line would typically be: }, "my error message");

请参阅小提琴: http://jsfiddle.net/jasonday/fWk5u/

解决方案

我最终弄明白了.返回成功是我失败的原因-我不断返回,返回true或返回false-并且其中每个都会失败"验证.

remote: {
    type: "POST",
    url: "js/username.json",
    contentType: "application/json; charset=utf-8",  
    dataType:"json",
    data: "{'" + $('#enterEmail').attr('id') + "': '" + $('#enterEmail').val() + "'}",
    dataFilter: function(data) {
        var json = JSON.parse(data);
        if(json.isError == "true") {
            return "\"" + json.errorMessage + "\"";
        } else {
            return success;
        }

    }
}

I'm trying to either use the remote method, or add a method that mimics the remote method to be able to send the entered data to a web service and get a json response back with the error state and error message.

so I have this json response:

{
    "isError": "true",
    "errorMessage": "The User Name you chose is already in use. Please enter another name."
}

I've used the remote method to get a true/false response, but not with extra data.

Any help would be appreciated.

Edit

Tried this, but of course it didn't work because the variable is not a global variable...

$.validator.addMethod("uniqueUserName", function(value, element) {

      $.ajax({
        type: "POST",
         url: "js/username.json",
         contentType: "application/json; charset=utf-8",  
        dataType:"json",
        data: "{'" + $('#enterEmail').attr('id') + "': '" + $('#enterEmail').val() + "'}", 
        dataFilter: function(data) {
            var isError = data.isError;
            var uniqueError = data.errorMessage;
            if(isError == "true"){
                return false;
            }
        }

     })

}, uniqueError); //this last line would typically be: }, "my error message");

see fiddle: http://jsfiddle.net/jasonday/fWk5u/

解决方案

I ended up figuring it out. The return success is what I was failing - I kept returning, returning true, or returning false - and every one of them would "fail" validation.

remote: {
    type: "POST",
    url: "js/username.json",
    contentType: "application/json; charset=utf-8",  
    dataType:"json",
    data: "{'" + $('#enterEmail').attr('id') + "': '" + $('#enterEmail').val() + "'}",
    dataFilter: function(data) {
        var json = JSON.parse(data);
        if(json.isError == "true") {
            return "\"" + json.errorMessage + "\"";
        } else {
            return success;
        }

    }
}

这篇关于jQuery验证-addMethod/remote-json响应中的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆