使用ASP.NET WebForms的jQuery Validator#remote

我正在尝试使用带有WebForms的jQuery Validator插件。 这是我的jQuery代码:

$("input[id$=FromZip]").rules('add', { required: true, postalCode: true, remote: { url: "shipping companies.aspx/ValidatePostalCode", type: "POST", contentType: "application/json; charset=utf-8", dataType: 'json', data: "{'postalCodeToValidate': '" + $("input[id$=FromZip]").val() + "'}", dataFilter: function(data) { return (JSON.parse(data)).d; } }, messages: { required: '"From" zip code is required.', postalCode: 'Invalid "From" zip code', remote: 'The "From" zip code was not found in the database.' } }); 

这个问题是val()总是返回空,所以值总是被标记为false。 我尝试在函数调用中包装它,如下所示:

 remote: function() { var r = { url: "shipping companies.aspx/ValidatePostalCode", type: "POST", contentType: "application/json; charset=utf-8", dataType: 'json', async: false, data: "{'postalCodeToValidate': '" + $("input[id$=FromZip]").val() + "'}", dataFilter: function(data) { return (JSON.parse(data)).d; } } } 

除了现在根本没有调用WebMethod并且没有进行Ajax请求,而之前(没有var r = {} stuff)正在进行调用,但是传递空数据而不是文本框中的值。 我一直试图在一天中的大部分时间里解决这个问题。

有什么建议?

你可以编写你的远程function

  remote: function() { var value = $('#FromZip').val(); var r = { contentType: "application/json; charset=utf-8", data: JSON.stringify({ 'postalCodeToValidate': value }), type: 'POST', dataType: 'json', url: 'shipping companies.aspx/ValidatePostalCode', dataFilter: function(data) { return $.parseJSON(data).d; } }; return r; }