简体   繁体   中英

Calling *.aspx function from remote HTML page

Hi I am new to web Application development. I have created a *aspx page which calls a webservice through a method which returns a string. Now I want to call this aspx method from remote HTML 5 page using any scripting language and display data on remote HTML page. Can any one please tell me how can I call a aspx method and display is content on HTML 5 page.

Following is code that I am using,

Default.aspx.cs file

   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

but when I call it from external HTML 5 it doesn't do any thing

HTML 5 code:

 <!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,

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

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

 $(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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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