1

I have a C# ASP.NET web service running (successfully) on this PC. I can test the LogonUser method via IIS and also call it from a test console application.

I have a second project, an HTML page that calls the web service using jQuery. This always fails and so quickly I don't think it's actually communicating with the service, making me think there's a syntax error in the JavaScript.

Have I got things configured correctly?

C# Web method:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class TimeRecordingService : System.Web.Services.WebService {

    [WebMethod]
    public string LogonUser(string _usercode, string _password) {
        ...             
    }
}

HTML source:

<body>
<form id="logon" action="">
<h1>
    Time Recording Logon</h1>
<p>
    <label>Usercode</label>
    <input id="usercode" type="text" />
</p>
<p>
    <label>Password</label>
    <input id="password" type="password" />
</p>
<p>
    <a type="submit" class="submit" id="LogonUser">Logon</a>
</p>
</form>
<!--Browsers block while requesting, loading, and executing JavaScript, defer this until as late as possible.-->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("a#LogonUser").click(function (e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'http://143.96.130.156/TimeRecording/TimeRecordingService.asmx/LogonUser',
                data: '{"_userCode": "' + $("input#usercode").val() + ", '_password': " + $("input#password").val() + '"}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: OnSuccess,
                error: OnError
            });
            function OnSuccess(data, status) {
                alert(status);
            }
            function OnError(request, status, error) {
                alert(status);
            }
        });
    });    
</script>

This is always returning an error immediately when I click the Logon button.

Can you see or think why?

Thank you, Aaron.

Aaron
  • 705
  • 10
  • 19
  • What kind of error are you getting? Have you tried Fiddler to see if there is any actual communication between the page and server? – Justin Largey Jan 13 '12 at 03:25

1 Answers1

0

this is because of the Same origin policy. you cannot use ajax to call external sites. if you really want to use, you have to use JSONP. Or you can use serverside proxy for this. means, call external site in the server side and do ajax call to the that webservice.

see my answer here

for more detail on how to call C# inside JavaScript see following links,

http://cmsnsoftware.blogspot.com/2010/11/simple-ajax-request.html

http://cmsnsoftware.blogspot.com/2011/01/how-to-call-csharp-function-in-ajax.html

http://cmsnsoftware.blogspot.com/2011/02/how-to-use-ajax-auto-complete-in-aspnet.html

Community
  • 1
  • 1
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • Thank you. During testing and production the web service and HTML files will be located on the same PC, running out of the same IIS web site, just different app pools and applications. Is it simply a configuration issue that they think they're on different sites? Thanks, Aaron. – Aaron Jan 13 '12 at 03:45
  • since you are accessing `url` with absolute url, it will treat as different sites(domains) use relative path for `url` – Chamika Sandamal Jan 13 '12 at 03:49