从JavaScript WCF服务方法+同步调用方法、JavaScript、WCF

2023-09-10 21:36:27 作者:无情ヾ待你如初ツ

我写了一个WCF服务,我试图调用服务方法的脚本在我的ASPX页面。

I have written a WCF service and am trying to call the service methods in a script in my ASPX page.

例如:

<script language="javascript" type="text/javascript">

<!--

        function Test() {
          **// The following call is an Async call.  
          tempuri.org.IService.GetData(1,OnRequestComplete, OnError, "");**
        }

        function OnRequestComplete(result, state) {
            var textBox = $get("txtInput");
            textBox.value = result;

        }
        function OnError(result) {
            var textBox = $get("txtInput");
            textBox.value = result;
        }

    //-->    
</script>

我要的是能够调用的服务方法同步

What I want is to be able to call the service method "synchronously"

例如: VAR的结果= tempuri.org.IService.GetData(1);

这可能吗?

推荐答案

我相信有没有能力在Javascript中做同步调用 - 总是返回在等待远程响应AJAX库。

I believe there's no ability to do synchronous calls in Javascript - the AJAX libraries will always return while waiting for a remote response.

你能解释一下你为什么要这么做?

Can you explain why you want to do this?

编辑:

在回答,你应该使用这个方法:

In answer, you should use this method:

在onclick事件处理程序的形式提交按钮:使Web服务验证调用,并立即返回false(这样的形式不提交)。 这将是显示给用户的确证类型的消息是一个好主意,让他们知道这里发生了什么。的

如果你得到一个有效的响应,然后用 document.form.submit(); 的形式提交到服务器。

If you get a valid response, then use document.form.submit(); to submit the form to the server.