2

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.

3
  • You could start by adding a error event to your ajax call and view the error details there Commented Dec 11, 2013 at 13:10
  • @RonnoDex: When I display any thing n default.aspx page contents appears on page but I need to display contents on HTML 5 page as per clients requirement Commented Dec 11, 2013 at 13:13
  • 1
    Yes, as I said add error: function(jqXHR, textStatus, errorThrown) { alert(errorThrown); } to your ajax call Commented Dec 11, 2013 at 13:15

2 Answers 2

3

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);
                }
            });
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer! Now it's returning data on remote HTML but the issue now I am facing is that it is returning undefined data! can you please let me know what is the issue and what could be done? I am totally new to this please any help is appreciated!
first return an simple string content as "Hello Test" or something like that, this is the test I did. Other problem could be the response type if the string is an xml or html you must specify in the content type of the ajax call so for xml it must be contentType: 'text/xml; charset=utf-8' and for html contentType: 'text/html; charset=utf-8'
0

Have you tried setting like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.