简体   繁体   English

从远程HTML页面调用* .aspx函数

[英]Calling *.aspx function from remote HTML page

Hi I am new to web Application development. 嗨,我是Web应用程序开发的新手。 I have created a *aspx page which calls a webservice through a method which returns a string. 我创建了一个* aspx页面,该页面通过一种返回字符串的方法来调用Web服务。 Now I want to call this aspx method from remote HTML 5 page using any scripting language and display data on remote HTML page. 现在,我想使用任何脚本语言从远程HTML 5页面调用此aspx方法,并在远程HTML页面上显示数据。 Can any one please tell me how can I call a aspx method and display is content on HTML 5 page. 谁能告诉我如何调用aspx方法并在HTML 5页面上显示内容。

Following is code that I am using, 以下是我正在使用的代码,

Default.aspx.cs file Default.aspx.cs文件

   using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using HotelTabWebServiceWebReference;
    using System.Web.Services;

    public partial class _Default : System.Web.UI.Page 
    {

      [System.Web.Services.WebMethod(true)]
      protected static string getHotelMenuList()
       {
            HotelTabWebServiceWebReference.HotelAppForTabWebService proxy = new HotelTabWebServiceWebReference.HotelAppForTabWebService();
            string menuList = null;  
          try
            {
                menuList = proxy.getMenuType();
            }
            catch (FormatException)
           {
               Console.Write("error");
           }
        return menuList;
        }
     }

When I run this default.aspx.cs page with default.aspx it shows works properly 当我运行带有default.aspx的default.aspx.cs页面时,它显示正常工作

but when I call it from external HTML 5 it doesn't do any thing 但是当我从外部HTML 5调用它时,它没有任何作用

HTML 5 code: HTML 5代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Untitled Page</title>
   <script type="text/javascript" src="jquery-1.10.2.js"></script>
   <script type="text/javascript" src="newJS.js"></script>

    </head>
     <body onload="sendSOAPRequest()">
      <div id="myDiv">data returned by aspx function should appear here</div>
      </body>
      </html>

JQuery code is as follow, jQuery代码如下,

// JavaScript Document
function sendSOAPRequest()
{


$.ajax({
    url: "http://localhost:50664/ASPX/Default.aspx/getHotelMenuList",
    type: "POST",
    dataType: "xml",
    data: "{}",
    complete: endFunction,
    async: true,
    contentType: "text/xml; charset=\"utf-8\"",
    success: function (msg) {
        $('#myDiv').text(msg.d); 
    }      

});
}
function endFunction(xmlHttpRequest, status)
{
    alert("complete"+xmlHttpRequest.re);
}

When executed HLML 5 file it executes endFunction but does not enters into success function. 当执行HLML 5文件时,它执行endFunction但不进入成功函数。

Please let me know how to do this. 请让我知道该怎么做。 Thanks in advance. 提前致谢。

In the aspx source you must have an script manager enabling the web methods you must put it within the form <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> I test it with the next jquery function 在aspx源中,您必须具有启用Web方法的脚本管理器,您必须将其置于<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">然后使用下一个jquery函数对其进行测试

 $(document).ready(function() {
            $.ajax({
                type: 'POST',
                url: 'Default.aspx/getHotelMenuList',
                data: '{ }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    alert(msg.d);
                }
            });
        });

您是否尝试过这样的设置:

contentType: 'text/xml; charset=utf-8'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM