2014-01-27 56 views
2

从他们的官方文档:香菜AJAX发送POST变量

 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请求我能做些什么?

+0

我想如果你把一个名字属性放在你的输入中,一个名字相同的变量和输入值将与发送请求一起发送。 – MamaWalter

+0

如果我想发送多个值,该怎么办? –

回答

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" /> 

那么你在后端脚本有验证 “测试” 变量和返回与200响应:

{ "success": "all good!" } 

{ "error": "your custom message" } 
+0

我可以发送多个值到远程目的地吗? –

+0

请回应后端文件的例子。 –

+0

更新 - 我只是试图使用远程验证,错误消息不会出现。为了让Parsley找出验证失败,我必须发出一个400返回码头。最重要的是,返回一个带有error属性的JSON对象(即使具有正确的内容类型头文件)也不会覆盖默认的错误消息。我最终添加了'data-parsley-remote-message =“我的字段远程验证错误消息。”。不知道这个答案是用于Parsley的旧版本还是API的变化,或者我只是做错了。 –

0

在较新版本的Parsley.js中,您可以使用如下所示的data-parsley-remote-options

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

把你需要发送的任何东西作为键/值对发送到data对象中。

更多信息在the docs