问题与jQuery验证插件(远程验证) [英] Problem with jQuery validate plugin (remote validation)
问题描述
我有一个问题,试图验证使用 jQuery的验证插件用户价值一>。
验证似乎正是正确触发,并调用Web服务的功能,我想,但,即使服务器功能不正常工作,并返回一个真正
/ 假
导致该领域始终是无效的。
这是在客户端验证code
$('#myForm的')。验证({
errorContainer:集装箱,
errorLabelContainer:$(OL,集装箱)
包装:礼,
元:验证,
规则:{
code:{要求:真正的,最大长度:15,远程:功能(){
返回{
键入:POST,
网址:GetBaseWSUrl()+'MyService.asmx / IsValid的code',
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
数据:JSON.stringify({umlt code:$('#code')VAL()})
}
}
},
说明:{要求:真正的,最大长度:255},
注:{最大长度:255}
},
消息:{
// ...为简洁起见省略
},
submitHandler:功能(形式){
saveObject(表);
}
});
使用招我能看到的呼叫向服务器发出,并且服务器返回一个JSON 真正
/ 假
值视情况如下面的示例中:
{D:虚假}
或
{D:真正}
尽管这样,插件仍标记字段为无效。任何建议?
修改:这是我的服务器功能
[WebService的空间(namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[ScriptService]
公共类为MyService:System.Web.Services.WebService
{
[的WebMethod]
公共对象的IsValid code(字符串umlt code)
{
[...] 如果(U!= NULL)
返回false; 返回true;
}
}
的问题是不是
真
Web服务回报
{D:真正}
的 D
属性应该被删除。
ASMX被认为是旧的,所以我将在第一个地方摆脱它。但在此之前,你也可以使用 dataFilter
财产,像这样的:
远程:功能(){
返回{
键入:POST,
网址:GetBaseWSUrl()+'MyService.asmx / IsValid的code',
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
数据:JSON.stringify({umlt code:$('#code')VAL()})
dataFilter:功能(数据){
VAR X =(JSON.parse(数据))D。
返回JSON.stringify(X);
}
};
}
I have a problem trying to validate a user value using the jQuery Validation plugin.
The validation seems to fire correctly and call the web service function exactly as I want but, even if the server function does work correctly and returns a true
/false
result the field is always invalid.
This is the validation code on the client side
$('#myForm').validate({
errorContainer: container,
errorLabelContainer: $("ol", container),
wrapper: 'li',
meta: "validate",
rules: {
Code: { required: true, maxlength: 15, remote: function () {
return {
type: "POST",
url: GetBaseWSUrl() + 'MyService.asmx/IsValidCode',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ umltCode: $('#Code').val() })
}
}
},
Description: { required: true, maxlength: 255 },
Notes: { maxlength: 255 }
},
messages: {
// ... omitted for brevity
},
submitHandler: function (form) {
saveObject(form);
}
});
Using fiddler I am able to see that a call is made to the server and that the server is returning a json true
/false
value depending on the case as in the following sample:
{"d":false}
or
{"d":true}
Despite this, the plugin still mark the field as invalid. Any suggestion?
EDIT: this is my server function
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public object IsValidCode(string umltCode)
{
[...]
if (u != null)
return false;
return true;
}
}
The problem is that instead of
true
your web service returns
{"d":true}
The d
property should be removed.
ASMX is considered legacy so I would get rid of it at the first place. But until then you could also use the dataFilter
property, like this:
remote: function() {
return {
type: "POST",
url: GetBaseWSUrl() + 'MyService.asmx/IsValidCode',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ umltCode: $('#Code').val() }),
dataFilter: function (data) {
var x = (JSON.parse(data)).d;
return JSON.stringify(x);
}
};
}
这篇关于问题与jQuery验证插件(远程验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!