2

I am having difficulty trying to get my field to validate using the remote rule with jQuery Validation. It is making the ajax call fine and passing the correct data. The Web service is operating fine and is returning a true or false as needed.

My problem comes in that the rule always stays negative. I am not sure if maybe I am returning the true/false in the incorrect format. Ideas/thoughts?

This is my rule:

$("[id$=txtOther]").rules("add", {
    remote: function () {
        return {
            type: "POST",
            url: $("[id$=hBaseURL]").val() + "Webservice/Validation.asmx/ValidateUser",
            data: JSON.stringify({ FullName: $("[id$=txtOther]").val(), UserID: $("[id$=txtOtherID]").val() }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
        };
        },

    messages: {
        remote: ""
    }
});

And this is how my web service is responding:

d       [Object {__type="Validation+Results", Status="true"}]
0       Object {__type="Validation+Results", Status="true"}
Status  "true"
__type  "Validation+Results"
divtag
  • 677
  • 2
  • 6
  • 21

2 Answers2

4

Well, this is what I ended up having to do to fix the problem. First I took what Andrew said and found more out there to support what he was saying. The most helpful was this post:

Problem with jQuery validate plugin (remote validation)

This post basically added these lines to my existing code:

dataFilter: function (data) {
    var x = (JSON.parse(data)).d;
    return JSON.stringify(x); 
}  

The problem was still there though. I started thinking about what could be wrong and started to wonder about my web service and how it was returning other than the .d response. I had used different modifications of this web service for some time now, but for some reason this one was not working. After looking a bit more at it I thought about the fact that it was returning a list of values since that is what I needed everywhere else. I was only returning one item in that list, but I was wondering what would happen if I returned just that item instead of the list. My web service now returned this:

{"d":"true"}

This still did not work with my original code, but now I was fairly certain it was because it was returning as d. The dataFilter I found at the other site was still not allowing it to work so I modified the dataFilter a little bit to return just plain text like this:

dataFilter: function (data) {
    var x = (JSON.parse(data)).d;
    return x;
}

Finally I had a solution that was working. During this process I ran into issues with getting the validation to trigger again after it failed, or supposedly returned true even though it was in error. There is a lot more information out there about this and since that wasn't the original question I won't go into all of that. One helpful post was: jquery validation - remote method won't trigger after valid However, just because they are so related I used this code to reset the validation allowing the validation to be triggered again even with a recent true valid call.

$("[id$=txtOther]").removeData("previousValue");

I am still using this code along with the following code to trigger the validation on demand:

var resetValid = $("[id$=txtOther]").valid();

This is my final code for the remote validation:

 $("[id$=txtOther]").rules("add", {
    remote: function () {
        return {
            type: "POST",
            url: $("[id$=hBaseURL]").val() + "Webservice/Validation.asmx/ValidateUser",
            data: JSON.stringify({ FullName: $("[id$=txtOther]").val(), UserID: $("[id$=txtOtherID]").val() }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            dataFilter: function (data) {
                var x = (JSON.parse(data)).d;
                return x;
            }
        };
    },
    messages: {
        remote: "Additional reviewers must be selected from available options"
    }
});
Community
  • 1
  • 1
divtag
  • 677
  • 2
  • 6
  • 21
2

It looks like your webservice is returning XML and the javascript is expecting JSON.

Could you try changing the return type of your service?

In the validator plugin change on line 975

response = response

to

response = response.d 
Andrew Walters
  • 4,763
  • 6
  • 35
  • 49
  • Sorry, you think I would have learned this the last time I posted a question about my web services. This is the output from the web service when called from the test interface. It is returning JSON and I have updated my question to reflect what the return is from firebug – divtag Sep 22 '12 at 02:03
  • I think it has something to do with the json being returned as d by Microsoft servers. I vaguely remember having to modify the validation plugin last time I was using the validator script. Let me see if I can dig up what I had to do. – Andrew Walters Sep 22 '12 at 02:08
  • Updated my answer with the plugin modification – Andrew Walters Sep 22 '12 at 02:10
  • Hmmmm, I can't find `response = response` anywhere in the plugin – divtag Sep 22 '12 at 02:27
  • Do you have the un-minified version? It should be on line 975. I might be wrong and I actually added that line, but check out that method on ln975 – Andrew Walters Sep 22 '12 at 02:45
  • I was looking at the un-minified version and didn't see this line and even searched for that line of code with no luck. I have found the solution which I will post tomorrow - thanks!! – divtag Sep 24 '12 at 01:22