【问题标题】:Parsley ajax sending post variablesParsley ajax 发送帖子变量
【发布时间】:2014-01-27 21:23:52
【问题描述】:

来自他们的官方文档:

        Custom ajax validation made simple.
parsley-remote-method="POST" to change default GET call.
parsley-remote-datatype="jsonp" if you make cross domain ajax call and expect jsonp
Parsley will accept these valid returns with a 200 response code: 1, true, { "success": "..." } and assume false otherwise
You can show frontend server-side specific error messages by returning { "error": "your custom message" } or { "message": "your custom message" }

但是,如果我想在 post 请求中发送变量,我该怎么办?

【问题讨论】:

  • 我认为,如果您在输入中放置名称属性,则输入具有相同名称的变量,输入值将与发布请求一起发送。
  • 如果我想发送多个值怎么办?

标签: javascript php ajax


【解决方案1】:

要在字段上触发远程验证器,您需要放置两个属性

parsley-remote="http://yoururl.com"

parsley-remote-method="POST"

使变量在 POST 请求中提交。

例如:

<input type="text" name="test" value="1" parsley-remote="http://yoururl.com" parsley-remote-method="POST" />

那么后端的脚本必须验证“test”变量并返回 200 响应:

{ "success": "all good!" }

{ "error": "your custom message" }

【讨论】:

  • 我可以向远程目的地发送多个值吗?
  • 请提供带有响应的后端文件示例。
  • 更新 - 我只是尝试使用远程验证,但不会出现错误消息。为了让 Parsley 找出验证失败,我必须发出 400 返回码标头。最重要的是,返回带有错误属性的 JSON 对象(即使具有正确的内容类型标头)也不会覆盖默认错误消息。我最终添加了data-parsley-remote-message="My field remote validation error message."。不确定这个答案是否适用于旧版本的 Parsley 或 API 更改,或者我只是做错了。
【解决方案2】:

在新版本的 Parsley.js 中,您可以像这样使用data-parsley-remote-options

data-parsley-remote-options='{
    "type": "POST",
    "dataType": "jsonp",
    "data": {
        "token": "value"
    }
}'

将您需要发送的任何内容作为键/值对放入data 对象中。

the docs 中的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多