2

i have the following jQuery function in my project

$.ajax({
            url: "WebServices/BrowserType.asmx/IsIE",
            contentType: "application/json; charset=utf-8",
            dataType: "text",
            type: "POST",
            data: {},
            success: function (msg) {
                If(msg.d== 'false')
                {
                    $("#Test2").html(msg);
                }
            },
            error: function (err) {
                alert("Error" + err.toString());
            }
        });

What i intend to do is to get a boolean value from my webservice and then act on the result on my jquery success option.

I had tried the following:

  • I set the dataType to boolean (of course, i changed my method return type to bool) and in the success function of the jquery, i had

    success:function(){if (msg.d){$("#Test2").html(msg);}}
    

but an error was thrown. (Object. That's the error.)

  • I tried setting the dataType to text (this time i changed my method return type to string) and in the success function of the jquery, i did it as in the above main code. This time there was no error but the success function seems not to work. However, when i removed the If statement from the above code, the success code worked.

Please how do i go about this. I had tried what i know. I intend to act on a boolean value. My web service returns either true or false and i want to act on the boolean value returned. The issue is how can i determine if the value returned is true or false in the success function of my ajax?

Your contribution is highly appreciated. I have edited this post to make it clearer.

4

2 回答 2

1

你试过用

if (returnData.d==true){//做点什么}else{//别做}

于 2012-08-07T13:09:24.343 回答
0

如果您的结果是文本,请尝试将其转换为布尔值。看一下这个:

如何在 JavaScript 中将字符串转换为布尔值?

于 2012-08-07T13:20:06.473 回答