0

I have a simple WebMethod call that does not return anything. I receive no errors.

The code is very simple. I just wonder if there is a setting (IIS, webconfig, etc.) that is not correct. I have VS Pro 2013, Framework = 4.5 and IIS 10.0 Express.

Any thoughts on why my "Hello World!" program does not work?

JavaScript.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication1
{
    public partial class JavaScript : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        public static string GetData()
        {
            return "Hello World!";
        }
    }
}

JavaScript.aspx.cs

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="WebApplication1.JavaScript" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">   
<head runat="server">

    <title></title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>
    $(document).ready(function () {

         $.ajax({
             type: "POST",
             url: "JavaScript.aspx/GetData",
             contentType: "application/json; charset=utf-8",
             dataType: "json",

             success: function (response) {
                 $("#Content").text(response.d);
             },

             failure: function (response) {
                 alert(response.d);
             }
         });
    });
    </script>

</head>

<body>

<form id="frm" method="post">

    <div id="Content">
    </div>

</form>
</body>
</html>

Since I cannot post images (Not enough Reputation Points), please click this link to see my Network Tab: Here is the Network Tab

enter image description here

12
  • 3
    Why are you doing a post, change it to get I hope that will work Commented Apr 7, 2017 at 13:46
  • 1
    Watch the network request using your browser's debugging tools. What is the status code of the response? What does the response content look like? Commented Apr 7, 2017 at 13:47
  • 1
    Does the webmethod work through debug mode? I can also recommend Postman for testing web services and debugging errors like this one - Very helpful to determine whether the problem is with Server or client. Commented Apr 7, 2017 at 13:50
  • 1
    You might also try formatting "Hello World!" as JSON. Commented Apr 7, 2017 at 14:26
  • 1
    I've tested your code and it seems to work fine, and according to your network tab the AJAX call seems to succeed. Perhaps could you share the contents of the response of the GetData call in your network tab? Commented Apr 7, 2017 at 14:37

0

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.