从 javascript 调用 webservice 并等待响应

发布于 2024-12-17 14:00:41 字数 697 浏览 4 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉风有信 2024-12-24 14:00:41

如果您考虑使用纯 JavaScript(现在很不寻常),请考虑这段代码

var strURL = "localhost/services/foobar.svc";
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
    self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
    if (self.xmlHttpReq.readyState == 4) {
        // do whatever you want to do when your service responded
        // you may get the response content using self.xmlHttpReq.responseText
    }
}
self.xmlHttpReq.send();

如果您考虑使用 jQuery (这个周围流行的 JavaScript 框架),很简单:

$.ajax({
  url: "localhost/services/foobar.svc",
  success: function(data){
    // this is fired on successful response
    // you will have response content in data parameter
  }
});

If you consider using pure JavaScript (pretty unusual these days), consider this code

var strURL = "localhost/services/foobar.svc";
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
    self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
    if (self.xmlHttpReq.readyState == 4) {
        // do whatever you want to do when your service responded
        // you may get the response content using self.xmlHttpReq.responseText
    }
}
self.xmlHttpReq.send();

If you consider using jQuery (this one of the popular JavaScript framework around), its easy:

$.ajax({
  url: "localhost/services/foobar.svc",
  success: function(data){
    // this is fired on successful response
    // you will have response content in data parameter
  }
});
肥爪爪 2024-12-24 14:00:41

使用 XmlHttpRequest (Ajax) 并在回调函数中处理响应。

Use XmlHttpRequest (Ajax) and handle the response in your callback function.

梦里南柯 2024-12-24 14:00:41

如果您想同步调用服务,那么您也可以从代码隐藏中调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文