Hi,
I tried calling a simple webservice using jquery in dotnetnuke but I get an error saying file not found (404). Please review my code
WEBSERVICE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
/// <summary>
/// Summary description for TestPost
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class TestPost : System.Web.Services.WebService {
public TestPost () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public Names[] GetNames()
{
List<Names> list = new List<Names>();
DataTable dt = DataStore.GetDataTable();
foreach (DataRow row in dt.Rows)
{
Names _names = new Names();
_names.FirstName = row["Name"].ToString();
_names.Age = row["age"].ToString();
list.Add(_names);
}
return list.ToArray();
}
}
public class DataStore
{
public static DataTable GetDataTable()
{
DataTable dt = new DataTable("Names");
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Age");
dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
DataRow dr1 = dt.NewRow();
dr1[0] = "Ahmed";
dr1[1] = "27";
DataRow dr2 = dt.NewRow();
dr2[0] = "Peter";
dr2[1] = "30";
DataRow dr3 = dt.NewRow();
dr3[0] = "John";
dr3[1] = "20";
DataRow dr4 = dt.NewRow();
dr4[0] = "Ali";
dr4[1] = "30";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
dt.Rows.Add(dr4);
return dt;
}
}
public class Names
{
private string _firstName;
private string _age;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string Age
{
get { return _age; }
set { _age = value; }
}
}
MARKUP
<%@ Control language="C#" Inherits="GlobalPay.Modules.MainBoard.ViewMainBoard" CodeFile="ViewMainBoard.ascx.cs" AutoEventWireup="true"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<%@ Register TagPrefix="dnn" TagName="Audit" Src="~/controls/ModuleAuditControl.ascx" %>
<script type="text/javascript">
jQuery = jQuery.noConflict();
jQuery(document).ready(function() {
jQuery("#Button1").click(function(event) {
jQuery.ajax({
type: "POST",
url: "TestPost.aspx/GetNames",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
Success: " + msg);
},
error: function(msg) {
Failed: " + msg.status + ": " + msg.statusText);
}
});
});
});
function AjaxSucceeded(result) {
result.d);
}
function AjaxFailed(result) {
result.status + ' ' + result.statusText);
}
</script>
<body>
<div id="container">
<asp:TextBox runat="server" ID="PractiseTextbox"></asp:TextBox>
<input id="Button1" type="button" value="button"/>
<div id="footer">
</div>
</div>
</body>
CODE BEHIND
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Web.Services;
using AjaxControlToolkit;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
namespace GlobalPay.Modules.MainBoard
{
partial class ViewMainBoard : PortalModuleBase
{
protected void Page_Load(object sender, EventArgs e)
{
DotNetNuke.Framework.jQuery.RequestRegistration();
//register the accordion script
if (!DotNetNuke.UI.Utilities.ClientAPI.IsClientScriptBlockRegistered(Page, "ui.core.js"))
{
DotNetNuke.UI.Utilities.ClientAPI.RegisterClientScriptBlock(Page,
"ui.core.js", "<script src=\"" + Request.ApplicationPath +
"/jquery-1.4.1.min.js\" type=\"text/javascript\"></script>");
}
ScriptManager objScriptManager;
ServiceReference objServiceReference;
if (DotNetNuke.Framework.AJAX.IsInstalled())
{
objScriptManager = ScriptManager.GetCurrent(this.Page);
objServiceReference = new ServiceReference();
objServiceReference.Path = ResolveUrl("TestPost.asmx");
objScriptManager.Services.Add(objServiceReference);
}
}
}
}