我已经阅读了很多关于jquery的内容,我有一个简单的web服务返回一个方法。现在我想在dotnetnuke中使用jquery调用该webservice但是我收到一个错误,说找不到文件(错误号404)。
这是我的代码:
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>
代码背后
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);
}
}
}
}
请查看我的代码,看看我是否做错了。
答案 0 :(得分:0)
在我看来,ASMX文件的路径不正确(因此404)。试试这个:
url: '<%=ResolveUrl("TestPost.asmx/GetNames")%>'
由于您是从DNN模块调用此函数,因此相对于当前模块控件的路径不太可能到达正确的位置;你需要相对于页面的路径,并且在编译时你不会知道。 ResolveUrl
会为您提供一个可在当前页面上运行的网址,无论其位于何处。