Tap*_*ose 7 javascript c# exception-handling asmx
我试图WebMethod从JavaScript 调用一个.到目前为止,我有:
EMSWebService.asmx:
namespace EMSApplication.Web.WebServices
{
/// <summary>
/// Holds the Webservice methods of EMSApplication
</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class EMSWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
Run Code Online (Sandbox Code Playgroud)
在aspx页面中,我添加了以下内容:
<asp:ScriptManager ID="ScriptManager" runat="server">
<Services>
<asp:ServiceReference Path="~/WebServices/EMSWebService.asmx" />
</Services>
</asp:ScriptManager>
<input onclick="callWebMethod();" id="btn" type="button" value="Click Me" />
Run Code Online (Sandbox Code Playgroud)
JavaScript是:
<script type="text/javascript">
function callWebMethod() {
EMSApplication.Web.WebServices.EMSWebService.HelloWorld(OnComplete, OnError);
}
function OnComplete(result) {
alert(result);
}
function OnError(result) {
alert(result.get_message());
}
</script>
Run Code Online (Sandbox Code Playgroud)
但该方法没有执行.我收到了以下JavaScript错误:
EMSApplication未定义.
有什么我想念的吗?我需要做一些其他配置吗?
项目结构如下所示:

JavaScript和组件位于Login.aspx中.
是否有任何重要的URL [WebService(Namespace = "http://tempuri.org/")]
编辑:
我也尝试过使用jQuery并将aspx页面修改为:
$(document).ready(function () {
$("#btn").click(function () {
$.ajax({
type: "POST",
url: "../WebServices/EMSWebService.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function (msg) {
alert(msg.d);
}
});
return true;
});
});
Run Code Online (Sandbox Code Playgroud)
我已经写System.Diagnostics.Debug.WriteLine("Hello World");里面的WebMethod,它正在执行,即,它是打印的"Hello World",在Visual Studio中的输出窗口,但我没有得到任何警告信息.
我想直接回答这个问题。
我有一个WebMethod坐在SomePage.aspx文件中:
[WebMethod]
public static String DoSomething(String shiftName)
{
return shiftName+" hi there";
}
Run Code Online (Sandbox Code Playgroud)
问题是:如何调用此网络方法?由于这是HTTP,因此答案是对服务器的HTTP POST:
POST http://localhost:53638/SomePage.aspx/DoSomething HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: qps-ploc,en-US;q=0.5
Accept-Encoding: gzip, deflate
Host: localhost:53638
Connection: Keep-Alive
Content-Length: 23
Content-Type: application/json;charset=utf-8
{'shiftName':'contoso'}
Run Code Online (Sandbox Code Playgroud)
要注意的至关重要的事情是:
POST (GET无效)您在aspx页面上将您的方法名称指定为SomePage.aspx / [MethodName]。在这种情况下:
SomePage.aspx / DoSomething
您将方法的参数作为JSON传递。此方法有一个字符串参数:shiftName。这意味着我构造了JSON:
{'shiftName':'contoso'}
Run Code Online (Sandbox Code Playgroud)使用请求的JSON内容类型,您必须指定Content-Type请求标头:
ContentType: application/json;charset=utf-8
Run Code Online (Sandbox Code Playgroud)假设我的示例WebMethod只是接受提供的文本,appends hi there并返回该字符串,则Web服务器的响应为:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 24
Connection: Close
{"d":"contoso hi there"}
Run Code Online (Sandbox Code Playgroud)
其中HTTP响应主体也是JSON字符串,具有一个称为的单个属性d。我不知道他们d从哪儿来的,但是在那里。
这就是使用http调用WebMethod的方式(例如,汇编语言,COM,C#,Java,Delphi)。
最常见的问题是如何使用 jQuery 从客户端调用它。
$.ajax({
type: "POST",
url: 'Catalogo.aspx/checaItem',
data: "{ id : 'teste' }",
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud)
注意:任何发布到公共领域的代码。无需注明出处。
小智 3
您需要确保在 web.config 中定义了 scripthandlerfactory...更多信息请参见http://msdn.microsoft.com/en-us/library/bb398998.aspx