从 javascript 调用 webservice 并等待响应
我创建了一个 webservice 并想从 javascript 调用它,为此我在页面的脚本管理器中注册了 ServiceReference ,并使用命名空间调用 webservice 。
我的问题是我想等到收到网络服务的响应。
为此,我尝试使用 setInterval 但这不起作用。
还有其他方法可以实现这一目标吗?
更多详情如下: 在 Javascript 中,如果您编写一个函数来调用 WebService,则必须向其传递一个方法签名,该方法签名将在 Webservice 发送响应后调用。
例如,
var IsResponseReceived;
function GetSomethingFromWebService()
{
IsResponseReceived = 'No';
Namespace.WebServiceClass.GetMeSomething(parameter1,SuccessResponseReceiver);
alert(webServiceResponse);
}
function SuccessResponseReceiver(parameter1,parameter2)
{
IsResponseReceived = 'Yes';
}
在上面的代码中,我的警报每次都给我“否”。我想在警报和网络服务调用之间写一些代码,让我等待得到响应。
我现在清楚了吗?
I have created a webservice and want to call it from javascript , for this I have registered ServiceReference in my page's script manager, and called webservice using namespace.
My problem is that I want to wait until response from webservice received.
For this I was trying to using setInterval but that does not work.
Any other way to achieve this?
More Details are below:
In Javascript If you write a function to call WebService you have to pass it a method signature that will be called after webservice sends response.
e.g.
var IsResponseReceived;
function GetSomethingFromWebService()
{
IsResponseReceived = 'No';
Namespace.WebServiceClass.GetMeSomething(parameter1,SuccessResponseReceiver);
alert(webServiceResponse);
}
function SuccessResponseReceiver(parameter1,parameter2)
{
IsResponseReceived = 'Yes';
}
In the above code my alert gives me 'No' everytime. I want to write someline inbetween alert and webservice call that should let me wait until I get response.
Am I Clear now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您考虑使用纯 JavaScript(现在很不寻常),请考虑这段代码
如果您考虑使用 jQuery (这个周围流行的 JavaScript 框架),很简单:
If you consider using pure JavaScript (pretty unusual these days), consider this code
If you consider using jQuery (this one of the popular JavaScript framework around), its easy:
使用 XmlHttpRequest (Ajax) 并在回调函数中处理响应。
Use XmlHttpRequest (Ajax) and handle the response in your callback function.
如果您想同步调用服务,那么您也可以从代码隐藏中调用 Web 服务。默认情况下,主线程会等待,直到您收到服务的响应。
If you want to call the service synchronously then you can call the webservice from code behind as well. By default the main thread would wait till you receive response from service.