为什么我的.NET webmethod返回一个完整的HTML文档而不仅仅是返回值?
我想要更熟悉AJAX和Web服务,所以我用VS2008创建了最简单的web服务,hello world,使用了webmethod GetPaper,并试图获得返回值“hello world”。为什么我的.NET webmethod返回一个完整的HTML文档而不仅仅是返回值?
<%@ WebService Language="C#" Class="HelloWorld" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class
HelloWorld : System.Web.Services.WebService {
[WebMethod]
public string GetPaper() {
return "Hello World";
}
}
http://www.linkedpapers.com/helloworld.asmx
然而,当我消耗此WebService使用Javascript,我得到一个完整的HTML页面结果,而不仅仅是价值!
xmlRequest.onreadystatechange = ApplyUpdate;
xmlRequest.open("GET", "http://www.linkedpapers.com/helloworld.asmx?op=GetPaper", true);
xmlRequest.send();
这可能很简单,但我似乎无法弄清楚!非常感谢帮助。
问候,
赫拉斯
编辑:还是我用错了网址?如果是这样,我应该使用什么?
应该更像这样:
xmlRequest.onreadystatechange = ApplyUpdate;
xmlRequest.open("GET", "http://www.linkedpapers.com/helloworld.asmx/GetPaper", true);
xmlRequest.send();
另外,还要确保您配置的web.config允许GET操作:
<webServices>
<protocols>
<add name=”HttpGet”/>
</protocols>
</webServices>
您可能希望查看在ASP.NET脚本管理器中使用Web引用而不是基本的GET请求。这篇文章将帮你:
感谢您分享该链接,并且最终我想使用支持AJAX功能的.NET,但是在开始创建更复杂的Web应用程序之前,我真的很想了解基本知识,真正发生了什么,我立即被困在最简单的任务中...... 我绝对会潜入你的链接,所以谢谢! – Heras 2010-02-01 14:09:31
我相信HTTP GET和POST默认是禁用的。 "INFO: HTTP GET and HTTP POST Are Disabled by Default"
谢谢!我将把Get和Post添加到web.config中,以确保我不会再次遇到同样的问题。 – Heras 2010-02-01 14:24:25
是的!这个组合做到了! 我试过“helloworld.asmx/GetPaper”,但在web.config中没有“add name = HttpGet”的情况下不起作用。 谢谢! – Heras 2010-02-01 14:23:41