Web Service utilizing ASMX, HTML, & Javascript

Copper Contributor

Hello All,

I'm struggling here. I think my WebService is programmed correctly because I get this page:
Page1.jpg
When I click Invoke, I indeed get a response:
Page2.jpg

So, I leave my code running in debug mode like so:
Page4.jpg
I then try to run my HTML/Java Script.  The Error if statement gets triggered and gives me back no data.

Page3.jpg

Here is my WebService Code:

 

 

 

 

Imports System.ComponentModel
Imports System.Web.Services
Imports System.Web.Services.Protocols

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
<ToolboxItem(False)>
Public Class WebService
    Inherits System.Web.Services.WebService

    <WebMethod()>
    Public Function HelloWorld(ByVal ToSomeone As String) As String
        Return "Hello World " & ToSomeone
    End Function

    <WebMethod()>
    Public Function GetDetails(name As String, age As Integer) As String
        Return String.Format("Name: {0}{2}Age: {1}{2}TimeStamp: {3}", name, age, Environment.NewLine, DateTime.Now.ToString())
    End Function


End Class

 

 

 

 

 

Here is my HTML/Java Code:

 

 

 

 

<!DOCTYPE html>
<html>
	<body>
		<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>Name:</td>
        <td><input type="text" id="txtName" /></td>
    </tr>
    <tr>
        <td>Age:</td>
        <td><input type="text" id="txtAge" /></td>
    </tr>
    <tr>
        <td><input type="button" id="btnSubmit" value = "Submit" /></td>
    </tr>
</table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#btnSubmit").click(function () {
            var name = $.trim($("#txtName").val());
            var age = $.trim($("#txtAge").val());
            $.ajax({
                type: "POST",
                url: "https://localhost:44388/WebService.asmx/GetDetails",
                data: "{ name: '" + name + "', age: " + age + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert(r.d);
                },
                error: function (r) {
                    alert(r.responseText);
                },
                failure: function (r) {
                    alert(r.responseText);
                }
            });
            return false;
        });
    });
</script>
    </body>
</html>

 

 

 

 

 

What am I doing wrong???

1 Reply

Hi @FallnGod,

Thanks for posting your issue here.

However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang