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

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 3
    Why are you doing a post, change it to get I hope that will work – Krishna Apr 07 '17 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? – mason Apr 07 '17 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. – grimdog_john Apr 07 '17 at 13:50
  • try changing `type: "POST"` to `type: "GET"`. Check your browser console for errors. Check your network tab to see what HTTP response is being given for the request (status code & message would be useful info), plus the content of the response body. In other words...do some basic debugging. "does not work" is not sufficient info. You wouldn't take yourself to the doctor and say "I'm not working properly". You'd say "my throat hurts" or whatever. The same applies to diagnosing code errors. – ADyson Apr 07 '17 at 14:08
  • Kris - changing to - type: "GET", - did not help. Thank you. – n00bie2112 Apr 07 '17 at 14:10
  • 1
    You might also try formatting "Hello World!" as JSON. – Crowcoder Apr 07 '17 at 14:26
  • kindly debug the whole process. if the call is being hit or not. If call is being hit then debug the ajax call's success and failure method. Use appendTo() jQuery method except .text() method. [link](http://stackoverflow.com/questions/15581059/how-to-add-text-to-an-existing-div-with-jquery) – Khalid Zubair Apr 07 '17 at 14:27
  • 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? – Ian Newson Apr 07 '17 at 14:37
  • I'm ***closing*** this, since the posted code is working for both Ian Newson and me. – Win Apr 07 '17 at 14:50
  • In my question, I state this may be more of a setting issue than a code issue. Please re-open. – n00bie2112 Apr 07 '17 at 17:13
  • @n00bie2112 I see you've posted your network tab, showing a 200 response. That's a good first step. Please double-click on the GetData request and look at the response data - is there anything inside the response body? – ADyson Apr 08 '17 at 05:19
  • @n00bie2112 Another thing to check would be your Console tab in chrome Dev tools. Just to see if there are any JS errors handling the response. – grimdog_john Apr 08 '17 at 09:46

0 Answers0