views:

333

answers:

1

I have what should be a relatively simple task that's frankly got me stumped. I've researched it until my brain is fried, and now I'm punting, and asking you guys for help.

Here's the scenario:

  • I have an ASPX page (Q2.aspx) that is decorated with the WebService, WebServiceBinding, and ScriptService attributes.
  • That page contains a method, GetAllContacts, that is decorated with the WebMethod attribute and returns a string containing JSON data. (For what it's worth, the page itself contains no other controls or functionality.)
  • I have an HTML page that contains JavaScript which uses the XmlHttpRequest object to invoke the GetAllContacts WebMethod on the ASPX page and transform the JSON data into an HTML table.
  • I have verified that my Web.Config file contains the appropriate protocol handlers for HttpGet and HttpPut in the WebServices section under System.Web.webServices.
  • I have verified that my Web.Config file contains the ScriptModule entry under the System.webServer.modules section, and that it matches the appropriate documentation.

However, when I view the HTML page in a browser, the following occur:

  • The web request goes through, but the results are for the unprocessed HTML from the ASPX page.
  • The GetAllContacts method is never invoked, as evidenced by setting a breakpoint in its code.
  • The code to invoke the Web service, however, is invoked, and the JavaScript callback function that is invoked upon request completion is properly invoked.

It appears that the JavaScript code is largely set up correctly, but for some reason that is completely escaping me at this point, the HTML page will simply not execute the WebMethod on the ASPX page, and simply returns the page as though it were a plain HTML GET request. Clearly, an HTML document can't be evaluated by JavaScript's eval function, which brings me to my problem. (Also note that the JSON data appears nowhere in the HTML that's returned.)

I am, frankly, baffled. I've looked at dozens of Microsoft articles, StackOverflow posts, CodeProject articles, and who knows what else. My code looks like it's okay. But I know better. I'm missing something simple, stupid, and obvious. I just need someone to point it out to me.

Below you'll find the ASPX page code and the HTML code, in the hope that they'll shed some light.

ASPX Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Q2.aspx.cs" Inherits="Satuit.Q2" enablesessionstate="False" %>
<html>
    <body>
        <form runat="server" id="frmMain"/>
    </body>
</html>
-- Codebehind
using System.IO;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace Satuit
{
    [WebService(Namespace="http://tempuri.org")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [ScriptService]
    public partial class Q2 : Page
    {

        [WebMethod]
        public static string GetAllContacts()
        {
            return LoadJsonData();
        }

        private static string LoadJsonData()
        {
            using (var stringWriter = new StringWriter())
            {

                string xmlUri = HttpContext.Current.Server.MapPath("\\XmlData\\Contacts.xml");
                string xslUri = HttpContext.Current.Server.MapPath("\\XmlData\\Q2.xsl");

                using (var xmlTextReader = new XmlTextReader(xmlUri))
                {
                    var xpathDocument = new XPathDocument(xmlTextReader);
                    var xslTransform = new XslCompiledTransform();

                    xslTransform.Load(xslUri);
                    xslTransform.Transform(xpathDocument, null, stringWriter);

                    return stringWriter.ToString();
                }
            }
        }
    }
}

HTML Code

    var objectData; // Receives the objectified results of the JSON request.

    var xmlhttp;
    if(window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }

    xmlhttp.open("GET", "/Q2.aspx/GetAllContacts", true);
    xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function () 
    {
        if (xmlhttp.readyState == 4) 
        {
            if (xmlhttp.status == 200)
            {
                var jsonResultBuffer = xmlhttp.responseText;
                objectData = eval(jsonResultBuffer);
                DisplayTable();
            }
        }
    };
    xmlhttp.send(null);

    function DisplayTable()
    {       
        var sHtml = "";     
        sHtml = "<table><tr><th>ID</th><th>First</th><th>Last</th><th>Address</th></tr>";           
        for(i = 0; i < objectData.length; i++)
        {
            sHtml += "<tr>";
            sHtml += "<td>" + objectData.ID;
            sHtml += "<td>" + objectData.firstName + "</td>";
            sHtml += "<td>" + objectData.lastName + "</td>";
            sHtml += "<td>" + objectData.address + "</td>"; 
            sHtml += "</tr>"
        }
        sHtml += "</table>"         
        document.getElementById("divTable").innerHTML = sHtml;
    }    
</script>

Dev Environment Details

  • Vista Ultimate SP 2
  • Visual Studio 2008
  • .NET Framework 3.5
  • Solution has not yet been deployed, so it's running in the "local Web server" provided by Visual Studio. (Makes me wonder if I shouldn't just deploy IIS under Vista.)
  • Note that the ASPX page containing the WebMethod and the HTML page reside within the same solution.
A: 

Pls try the following using jquery to see the web service is accessible or not.

$.ajax({
        type: "POST",
        url: "Q2.aspx/GetAllContacts",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
           alert("success");
        },
        error: function(response, aa) {
            alert("fail");
        }
    });

Thurein

Thurein
(Deleted for poster stupidity.)
Mike Hofer