简体   繁体   English

jQuery验证程序,使用GET而不是POST的远程规则?

[英]JQuery Validator, remote rule with GET instead of POST?

I am trying to do a remote validation with JQuery Validator: 我正在尝试使用JQuery Validator进行远程验证:

rules: {
        username: {
            minlength: 6,
            maxlength: 12,
            remote: {
                url: '/users/register/isUserAvailable',
                dataType: 'POST',
                data: {
                    'username': $('#username').val()
                },
                success: function(data) {
                    // success tasks...
                }

            }
        },

I want to see if there is a way to do the validation with GET instead of POST to have it like this: 我想看看是否有一种使用GET而不是POST进行验证的方法,如下所示:

rules: {
            username: {
                minlength: 6,
                maxlength: 12,
                remote: {
                    url: function(){
                        var username = $('#username').val();
                        return '/users/register/isUserAvailable/' + username,
                    } 
                    dataType: 'GET',
                    success: function(data) {
                        // success tasks...
                    }

                }
            },

NOTE: I know post is secure & is currently working fine, but I was just wondering if this was possible. 注意:我知道帖子是安全的,并且目前可以正常工作,但是我只是想知道是否可行。

There are a few syntax problems in your examples 您的示例中有一些语法问题

  1. to change from GET to POST the parameter name is type , not dataType 从GET更改为POST,参数名称为type ,而不是dataType
  2. the url parameter is not evaluated as a function, it is a string. url参数不作为函数求值,它是一个字符串。 This would explain why you are getting weird characters, they are the text of your function urlencoded. 这可以解释为什么您会收到奇怪的字符,因为它们是您的函数的文本,采用了urlencoded。

Two more points though 不过还有两点

  1. The default is of the remote method is GET, so you don't need specify the type for GET 远程方法的默认值为GET,因此您无需指定GET的类型
  2. Be careful overriding success in the remote method, unless you know the workings of the success callback of the remote method don't do it. 在远程方法小心覆盖成功 ,除非你知道了远程方法不这样做的成功回调的工作。

from the docs keep it simple like so 从文档中保持它像这样简单

$( "#myform" ).validate({
  rules: {
    email: {
      remote: {
        url: "check-email.php",
        type: "post",
        data: {
          username: function() {
            return $( "#username" ).val();
          }
        }
      }
    }
  }
});

我想对此没有限制,但是您需要将dataType: 'GET'更改为type: 'GET'

Your remote syntax is incorrect, there is no success , the remote only expects a boolean value based on your supplied data 您的远程语法不正确,没有成功,远程仅根据您提供的数据期望布尔值

remote: {
     type: 'GET',
     url: '/users/register/isUserAvailable',
     data: {
      username: function() {
        return $( "#username" ).val();
      }
            }//end remote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM