1

I have an ASP.NET Web Forms application and on one of my .ASPX pages I have some javascript.

In one part of the Javascript, I want to call a web service so I can perform a server-side function and return either a true or false value, then in the javascript I can perform one of two actions depending on the true or false value.

I've not done any web services before and so I'm struggling with getting a simple value back to the JS.

My WebService (LoginCheck.asmx) currently looks something like this:

[System.Web.Script.Services.ScriptService]
public class LoginCheck : System.Web.Services.WebService
{
    [WebMethod]
    public bool IsLoggedIn()
    {
        return UserService.IsAuthenticated();
    }
}

My Javascript call currently looks something like this:

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "LoginCheck.asmx/IsLoggedIn",
    data: "{}",
    dataType: "json",
    success: function (response) {
        if (response) {
            alert('Logged in: true');
        } else {
            alert('Logged in: false');
        }
    }
});

Despite having a breakpoint in my code on the 'IsLoggedIn' web service method, it never breaks on it. I have checked the 'console' in Google Chrome when the web service is supposed to be called and it says something along the lines of:

"The following operations are supported...", and it shows my one 'IsLoggedIn' method, but all the content rendered in this console window is essentially an HTML page, with HTML markup.

I don't appear to ever be a) breaking on the breakpoint in code or b) getting a response back to my javascript (hence neither of the js alerts shown in my code above are occurring).

2 Answers 2

3

it's getting a 301 redirect back to the LoginCheck.asmx

I got the same problem today.

You are probably using a LowerCaseRule in the URLRewrite 2 module for IIS. Your method name is IsLoggedIn, so the LowerCaseRule tries to call isloggedin, which fails.

Your LowerCaseRule should ignore asmx files, like this:

        <rule name="LowerCaseRule1" stopProcessing="true">
            <match url="[A-Z]" ignoreCase="false" />
            <conditions>
                <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml)|(asmx))$" ignoreCase="true" negate="true"/>
            </conditions>
            <action type="Redirect" url="{ToLower:{URL}}" />
        </rule>

Edit: I summed some of today's experience up and posted it here: http://www.tomot.de/en-us/article/8/asp.net/how-to-use-jquery-to-call-a-webservice-asmx-method

Sign up to request clarification or add additional context in comments.

4 Comments

This sounds like it could be the issue as I do have thar lowercase rule in place. I will try this when I get back to work in 2 weeks and update this question with the outcome. Many thanks for the help.
Perfect - This condition within the LowerCaseRule fixed things, thanks.
Thanks SO much for this.. You've no idea how much time I spent fighting with this problem
@Lars Holdgaard: I'm glad I could be of assistance ;)
2

You have to POST your request and don't send empty data:

$.ajax({
    type: "POST", // CHANGED
    contentType: "application/json; charset=utf-8",
    url: "LoginCheck.asmx/IsLoggedIn",
    //data: "{}", REMOVED
    //dataType: "json", REMOVED, NOT NEEDED
    success: function (response) {
        if (response.d) { // CHANGED
            alert('Logged in: true');
        } else {
            alert('Logged in: false');
        }
    }
});

Then you get a object response with the properties of whatever your method returns.

If it is bool it will look like this:

Object
d: true

So in response.d you have your bool to proceed with.

6 Comments

The removal of those lines and changing it to post does now appear to at least drop into the js alerts off the back of calling the asmx, but a) my breakpoint isn't getting hit in the asmx, and alerting 'response.d' in the js gives me 'undefined' despite the methods return type being a boolean. Any ideas?
Also, if I call alert(response) in the js, it appears to contain an html document (just loads of html markup) which I think contains 'The following operations are supported...', it has LoginCheck.asmx?disco or similar to that in the HEAD part of the markup.
I put this on my locale machine to check and it worked fine. There must be something wrong in your request. If it says 'The following operations are supported' it means the function call is wrong. Try to open LoginCheck.asmx direct in a browser and it should list you the possible methods. Then select your action and click invoke - and debug what the browser is sending to your service (with dev tools in chrome as example).
Oh and maybe remove the contentType, but it works fine for me with it.
Using Fiddler, it looks like when I click the invoke button, it's getting a 301 redirect back to the LoginCheck.asmx page (without the method name being /appended to the url) - every time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.