我有一个.NET 3.5 aspx的地方,其方法标有该[WebMethod]
属性.我用jQuery调用它,在两个方向发送JSON.一切都很好.我的问题是,[ScriptMethod]
应用于方法时会怎么做?我试过这个,它似乎产生了相同的结果.是ScriptMethod
和WebMethod
相同的,可互换的,还是一个提供功能和/或开销,其他不?总的来说,我发现自己与实现Web服务的所有选项相混淆,我想知道每个选项的优缺点.
jQuery 1.3.2,ASP.NET 2.0.对PageMethod(WebMethod)进行AJAX调用会返回完整/整个页面,而不仅仅是响应.页面方法上的断点显示它永远不会被击中.我的方法有[WebMethod]属性,它是 public static,返回string并且不接受params.我甚至尝试在我的班级顶部添加[ScriptService]以查看它是否有帮助,但事实并非如此.
我已经看到这篇帖子Jquery AJAX与ASP.NET WebMethod返回整个页面有相同的症状,但我仍然有问题.我读了http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/,我觉得我跟着这个到了T,但仍然没有运气.
我正在制作的jQuery调用是:
jQuery.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
url: 'MyPage.aspx/SomePageMethod',
success: function(result){
alert(result);
}
});
Run Code Online (Sandbox Code Playgroud)
根据FF3中的Firebug,请求/响应头如下所示
Response Headers
Server ASP.NET Development Server/8.0.0.0
Date Tue, 24 Feb 2009 18:58:27 GMT
X-AspNet-Version 2.0.50727
Cache-Control private
Content-Type text/html; charset=utf-8
Content-Length 108558
Connection Close
Request Headers
Host localhost:2624
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset …
Run Code Online (Sandbox Code Playgroud) 在研究这个问题时,大多数SO问题都是关于该static
方法作为修复.
因为它不使用真正的(有点复杂的)WebMethod我刚刚创建了一个简单的,以便检查是否可以达到方法本身.
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static string HelloWorld()
{
return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)
电话.
<script>
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "usersWebMethods.aspx/HelloWorld",
dataType: "json",
success: function (data) {
alert(data.d);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
它总是归结为 500 (Internal Server Error)
Unknown web method HelloWorld.
Parameter name: methodName
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it …
Run Code Online (Sandbox Code Playgroud) 我知道我可以使用以下语法使用jquery调用页面方法
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
Run Code Online (Sandbox Code Playgroud)
这适用于aspx页面但是可以使用ascx页面吗?(网页控制)
我已经尝试了大约半个小时,因为我无法让它工作,我想知道它是否可能.
注意:为了清楚,当我尝试调用ascx页面时,我正在更新jquery中的url :)
我正在看一些我只能假设一次工作的旧代码.
MyPage.aspx:
function GetCompanyList(officeId) {
var companyList = document.getElementById('<%= CompanyDropDown.ClientID %>');
if (companyList.length == 0)
PageMethods.GetCompanyList(officeId, OnGetCompanyList);
else
EditCompany();
}
Run Code Online (Sandbox Code Playgroud)
和:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Run Code Online (Sandbox Code Playgroud)
代码背后:
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
return (
from c in Repository.Query<Company>()
where !c.IsDeleted && c.TypeEnumIndex == (short)CompanyRelationshipType.Hotel
select new CompanyMinimum() {
id = c.Id,
desc = c.Description
}
).ToList();
}
Run Code Online (Sandbox Code Playgroud)
但是,PageMethods.GetCompanyList()
在第一个代码段的调用中,Chrome会报告:
PageMethods未定义
谁能看到改变了什么来防止这种情况发生?
注意:我发现了类似的问题,但它们似乎都与此代码无关,无法在母版页或用户控件中使用,这不是这里的情况.
我的.cs中有这样的方法:
[System.Web.Services.WebMethod]
public static void GetServiceInformation(IInfo x) //IInfo is an interface
{
x.l_power = true;
x.lb_InboxCount = UserTrans.GetInbox(int.Parse(emp_num), 0);
}
Run Code Online (Sandbox Code Playgroud)
现在我想通过javascript方法调用此方法作为页面方法,但它不起作用.
<script type ="text/javascript">
function GetInfo() {
PageMethods.GetServiceInformation(this);
}
window.onload = setTimeout("GetInfo()", 3000);
</script>
Run Code Online (Sandbox Code Playgroud)
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true">
</telerik:RadScriptManager>
Run Code Online (Sandbox Code Playgroud)
我的.cs:
public partial class AppMaster : Log, IInfo //My page
{
public string Inbox
{
get
{
return hpl_Inbox.NavigateUrl;
}
set
{
hpl_Inbox.NavigateUrl = value;
}
}
public string Draft
{
get
{
return hpl_Draft.NavigateUrl;
}
set
{
hpl_Draft.NavigateUrl = value; …
Run Code Online (Sandbox Code Playgroud) 我正在构建一个动态的部分加载asp.net页面,我想用jQuery来调用页面方法或web服务来检索内容HTML.
页面方法或Web服务,性能明智,哪种方式更好?
如果我调用页面方法,在服务器端,页面是否经历了整个生命周期?
是否有任何好的资源可以帮助我更好地理解页面方法?
我有一个主页面,其中有一些页面方法被调用来执行一些活动.一个popuppanel(popuppanel内容页也有pagemethods)使用该主页内所展现的一些细节.如果同一被执行多次(即,打开弹出面板多次),它工作在所有其他浏览器比其他IE9(IE8中甚至工作).然而,第一次执行是成功的.下面提供了正在使用的代码.
Scriptmanager使用如下:
<ajaxToolkit:ToolkitScriptManager runat="server" ID="TSCM1" EnablePageMethods="true" />
主页中的脚本:
function Clkd(){
var ppnl=document.getElementById("if1");
ppnl.src="Test1.aspx";
$find('<%= MPE.ClientID %>').show();
}
function Clkd2(){
var ppnl=document.getElementById("if1");
ppnl.src="";
$find('<%= MPE.ClientID %>').hide();
}
$(document).ready(function(){
PageMethods.mainPageMethod("MainTest",cbackfn);
});
function cbackfn(str){
alert(str);
}
Run Code Online (Sandbox Code Playgroud)
页面方法:
[System.Web.Services.WebMethod(EnableSession = true)]
public static string mainPageMethod(String mainStr)
{
return mainStr + " Ok";
}
Run Code Online (Sandbox Code Playgroud)
Test1.aspx页面详细信息(这是弹出面板中加载的页面):
脚本代码如下:
$(document).ready(function(){
PageMethods.Testpm("Test",fnd);
});
function fnd(str){
alert(str);
}
Run Code Online (Sandbox Code Playgroud)
页面方法:
[System.Web.Services.WebMethod(EnableSession = true)]
public static string Testpm(String …
Run Code Online (Sandbox Code Playgroud) javascript c# modalpopupextender pagemethods internet-explorer-9
我的Pagemethod实施在Chrome浏览器中无效.我在VS 2008中开发了ASP.NET 3.5 Web应用程序.
以下代码不适用于chrome或Safari:
function FetchDataOnTabChange(ucName)
{
PageMethods.FetchData(ucName, OnSuccessFetchDataOnTabChange, OnErrorFetchDataOnTabChange);
}
function OnErrorFetchDataOnTabChange(error)
{
//Do something
}
function OnSuccessFetchDataOnTabChange(result)
{
//Do something
}
Run Code Online (Sandbox Code Playgroud) 嗨我从javascript调用一个简单的页面方法,这是我在标记处的代码
function OnCallSumComplete(result, userContext, methodName) {
alert(result);
}
function OnCallSumError(error, userContext, methodName) {
if (error !== null) {
alert(error.get_message());
}
}
function test(){
var contextArray = "";
PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError, contextArray);
}
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
Run Code Online (Sandbox Code Playgroud)
在cs
[System.Web.Services.WebMethod]
public static string TestMethod(string para)
{
return "Yes this is working";
}
Run Code Online (Sandbox Code Playgroud)
警报显示结果,并显示"null".我检查firebug,我没有看到控制台的错误.
如果我将TestMethod更改为
[System.Web.Services.WebMethod]
public static string TestMethod()
{
return "Yes this is working";
}
Run Code Online (Sandbox Code Playgroud)
和PageMethod到
PageMethods.TestMethod( function (response) { alert(response); } );
Run Code Online (Sandbox Code Playgroud)
它显示正确的响应为"是这是有效的".但是,我需要将参数传递给函数.我什么都想念?
感谢帮助.
pagemethods ×10
asp.net ×9
ajax ×4
javascript ×4
jquery ×4
web-services ×4
c# ×3
asmx ×2
asp.net-ajax ×1