调用的WebMethods与XmlHtt prequest和纯JavaScriptXmlHtt、WebMethods、JavaScript、prequest

2023-09-11 01:14:29 作者:路还长、别猖狂

我有什么应该是坦白地说,让我难住了一个相对简单的任务。我研究过它,直到我的大脑是油炸的,现在我撑船,并要求你们的帮助。

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.

下面的情况:

在我有一个ASPX页面(Q2.aspx),其装饰与的WebService WebServiceBinding ScriptService 属性。 在该网页包含了一个方法, GetAllContacts ,即饰有的WebMethod 属性并返回一个包含JSON数据的字符串。 (对于它的价值,页面 本身不包含其他控件或功能。) 在我有一个包含的JavaScript使用HTML页中的 XmlHtt prequest 对象调用 GetAllContacts 的WebMethod的ASPX页面和改造 JSON数据转换为HTML表。 我已经验证了我的 Web.config中文件包含相应的协议处理程序 为 HTTPGET HttpPut 下的 Web服务部分 System.Web.webServices 。 我已经验证了我的 Web.config中文件包含在了 ScriptModule System.webServer.modules 部分,它相应的文档匹配。 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.

然而,当我在浏览器中查看HTML页面,将发生以下情况:

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

在该网站的请求通过,但结果是从ASPX页面未处理的HTML。 的 GetAllContacts 方法不会被调用,就证明了其code设置一个断点。 的code调用Web服务,但是,被调用,而JavaScript回调 被请求完成后调用的函数被调用正确。 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.

看来,JavaScript的code主要是设置正确,但由于某些原因,完全是在这一点上逃避我,HTML页面根本不会执行的WebMethod ASPX页面上,只是返回的页面,就像它是一个普通的HTML GET 请求。很显然,一个HTML文档无法通过JavaScript的评估功能,这使我想到我的问题进行评估。 (另请注意,JSON数据无处出现在所返回的HTML。)

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.)

我坦率地说,百思不得其解。我已经看了几十个微软的文章,StackOverflow的帖子,$ C $的CProject的文章,谁知道还有什么。我的code的看起来的喜欢它的好。但我知道更好。我失去了一些东西简单,愚蠢的,和明显。我只是需要有人指出来给我。

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.

下面你会发现ASPX页面code和HTML code,希望他们会提供一些​​线索。

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>

         

开发环境细节

在Vista旗舰版SP 2 在Visual Studio 2008中 在.NET Framework 3.5的 在解决方案尚未展开,因此它在本地Web服务器运行 由Visual Studio提供。 (让我知道如果我不应该只部署IIS Vista下。) 请注意,包含的WebMethod和HTML页面的ASPX页面驻留在 相同的解决方案。 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.

推荐答案

请尝试以下操作使用jQuery看到Web服务访问或没有。

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