javascript 如何调用WebMethod?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9854006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call WebMethod?
提问by Tapas Bose
I am trying to call a WebMethod
from JavaScript. So far I have:
我正在尝试WebMethod
从 JavaScript调用 a 。到目前为止,我有:
The EMSWebService.asmx:
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";
}
}
}
In the aspx page I have added the followings:
在 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" />
And the JavaScript is:
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>
But the method is not executing. I am getting following JavaScript error:
但是该方法没有执行。我收到以下 JavaScript 错误:
EMSApplication not defined.
EMSApplication 未定义。
Is there anything I am missing? Do I need to do some other configuration?
有什么我想念的吗?我需要做一些其他的配置吗?
The Project structure is depicted below:
项目结构如下图所示:
JavaScript and components is in Login.aspx.
JavaScript 和组件位于 Login.aspx 中。
Is there any significance of the URL of [WebService(Namespace = "http://tempuri.org/")]
网址有什么意义吗 [WebService(Namespace = "http://tempuri.org/")]
Edit:
编辑:
I have also tried this by using jQuery and modify the aspx page as:
我也尝试过使用 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;
});
});
I have written System.Diagnostics.Debug.WriteLine("Hello World");
inside the WebMethod
, it is executing that i.e., it is printing "Hello World" in the Output Window of the Visual Studio but I am not getting any alert message.
我写System.Diagnostics.Debug.WriteLine("Hello World");
在里面WebMethod
,它正在执行,即它正在 Visual Studio 的输出窗口中打印“Hello World”,但我没有收到任何警报消息。
采纳答案by Jeremy Howard
You need to make sure that you have the scripthandlerfactory defined in your web.config...more here http://msdn.microsoft.com/en-us/library/bb398998.aspx
您需要确保在您的 web.config 中定义了 scripthandlerfactory ……更多在这里http://msdn.microsoft.com/en-us/library/bb398998.aspx
回答by Ian Boyd
I wanted to answer the question directly.
我想直接回答这个问题。
I have a WebMethod
, sitting in a SomePage.aspx
file:
我有一个WebMethod
, 坐在一个SomePage.aspx
文件中:
[WebMethod]
public static String DoSomething(String shiftName)
{
return shiftName+" hi there";
}
The question is: How do i call this web method?Since this is HTTP, the answer is to to an HTTP POSTto the server:
问题是:我如何调用这个网络方法?由于这是 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'}
The criticallyimportant things to note are:
需要注意的关键事项是:
- HTTP method:
POST
(GET
will not work) you specify the name of you method on the aspx page as SomePage.aspx/[MethodName]. In this case:
SomePage.aspx/DoSomething
you pass the parameters of the method as JSON. This method has one string parameter:
shiftName
. This means i constructed the JSON:{'shiftName':'contoso'}
with the request's JSON content type, you have to specify the
Content-Type
request header:ContentType: application/json;charset=utf-8
- HTTP方法:
POST
(GET
无效) 您在 aspx 页面上将方法的名称指定为SomePage.aspx/ [MethodName]。在这种情况下:
SomePage.aspx/DoSomething
您将方法的参数作为 JSON 传递。此方法有一个字符串参数:
shiftName
. 这意味着我构建了 JSON:{'shiftName':'contoso'}
对于请求的 JSON 内容类型,您必须指定
Content-Type
请求标头:ContentType: application/json;charset=utf-8
Given that my example WebMethod simply takes the supplied text, appends hi there
, and returns that string, the response from the web-server is:
鉴于我的示例 WebMethod 仅接受提供的文本、追加hi there
并返回该字符串,来自网络服务器的响应是:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 24
Connection: Close
{"d":"contoso hi there"}
Where the HTTP response body is also a JSON string, with a single property called d
. I don't know where they got d
from, but there it is.
其中 HTTP 响应正文也是一个 JSON 字符串,具有一个名为d
. 我不知道他们是从哪里来的d
,但它就在那里。
That's how you call a WebMethod using http (e.g. assembly language, COM, C#, Java, Delphi).
这就是您使用http(例如汇编语言、COM、C#、Java、Delphi)调用WebMethod 的方式。
The most common question is how do you call it from the client usingjQuery.
最常见的问题是如何从客户端使用jQuery调用它。
$.ajax({
type: "POST",
url: 'Catalogo.aspx/checaItem',
data: "{ id : 'teste' }",
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
}
});
Note: Any code released into public domain. No attribution required.
注意:任何发布到公共领域的代码。无需归属。