【发布时间】:2014-01-18 18:29:41
【问题描述】:
我有两个项目:
ASP.net 网站项目:
默认.aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type = "text/javascript">
function DisplayMessageCall() {
$.ajax({
type: "POST",
url: "http://localhost:24218/Service1.asmx/HelloWorld",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
}
function OnSuccessCall(response) {
$('#<%=lblOutput.ClientID%>').html(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
</script>
<h2>Example 1: Call Webservice using JQuery AJax (Without Input)</h2>
<asp:Button ID="btnGetMsg" runat="server" Text="Click Me" OnClientClick="DisplayMessageCall();return false;" /><br />
<asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>
和 ASP Web 服务应用程序:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
我在我的 ASP 网站中添加了我的 webService 的引用,但是当我单击按钮时,我收到了错误消息。 当我输入网址时
(http://localhost:24218/Service1.asmx/HelloWorld)
在浏览器中我得到以下结果:
<string xmlns="http://tempuri.org/">Hello World</string>
我使用 ASP.NET 4.5。
【问题讨论】:
-
ASMX 是一项遗留技术,不应用于新开发。 WCF 或 ASP.NET Web API 应该用于 Web 服务客户端和服务器的所有新开发。一个提示:Microsoft 已停用 MSDN 上的 ASMX Forum。
标签: c# asp.net json asmx service-reference