通过AJAX消费WCF服务 [英] Consuming a WCF Service via AJAX

查看:81
本文介绍了通过AJAX消费WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近一直在C3和VS2010中使用WCF,并希望通过AJAX ASP.NET网站将它们结合在一起.

问题是:

当WCF在单独的域中运行时,实现Web浏览器直接通过JavaScript访问WCF服务的最佳方法是什么?

在这种情况下,我的WCF服务在单独的域(应用程序层)中运行,因此用户无法从其Web浏览器直接访问WCF主机的URL.

服务器端调用(ASP.NET应用程序访问WCF服务器)非常容易实现,因此我通过编写启用了存根" AJAX的WCF服务(直接从应用程序层调用(从用户中提取))扩展了此功能. .但这要求我保持存根最新.因此,我正在寻找一种更好的方法.

任何想法或方向将不胜感激.

可以在以下位置找到基本图:http://andrew.net.au/~rowan/wcf.jpg

-R

So I have been playing with WCF recently in C3 and VS2010 and wanted to get this all stuck together with an AJAX ASP.NET website.

The question is:

What is the best way to implement a Web Browser accessing a WCF Service directly via JavaScript when that WCF is running in a separate domain?

In this case my WCF service is running in a separate domain (the application tier) and so the user can not directly access the URL of the WCF Host from their Web Browser.

Server side calls, (where the ASP.NET application accesses the WCF Server), were incredibly easy to achieve and so I extended this by writing a "stub" AJAX enabled WCF Service which called the Application Tier directly (abstracting it from the user). But this requires me to keep the stub up to date. So I am looking for a better way to do this.

Any ideas or direction would be appreciated.

A rudimentary diagram is available at: http://andrew.net.au/~rowan/wcf.jpg

-R

推荐答案

可以通过对xmlhttp对象进行najax调用来查找另一种方法.
jsut就像

One more method you can look by ajax call by mnaking xmlhttp object.

jsut like as

// base url for the ajax call to service
var baseUrl = "http://localhost:54976/RestServiceImpl.svc/";
//Ajax request function for making ajax calling through other object
function AjaxRequest(baseurl, type, callbackResponse, parameterString) {
    this.BaseURL = baseurl;
    this.Type = type;
    this.Callback = callbackResponse;
    this.createXmlRequestObject();
    this.ParemeterString = parameterString;
}
// Create XMLHTTP OBJECT
AjaxRequest.prototype.createXmlRequestObject = function() {
    if (window.ActiveXObject) { // INTERNET EXPLORER
        try {
            this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            this.xmlHttp = false;
        }
    }
    else { // OTHER BROWSERS
        try {
            this.xmlHttp = new XMLHttpRequest()
        } catch (f) {
            this.xmlHttp = false;
        }
    }
    if (!this.xmlHttp) { // RETURN THE OBJECT OR DISPLAY ERROR
        alert('there was an error creating the xmlhttp object');
    } else {
        //return this.xmlhttp;
    }
}
AjaxRequest.prototype.MakeRequest = function() {
    try {
        // PROCEED ONLY IF OBJECT IS NOT BUSY
        if (this.xmlHttp.readyState === 4 || this.xmlHttp.readyState === 0) {
            // EXECUTE THE PAGE ON THE SERVER AND PASS QUERYSTRING
            this.xmlHttp.open(this.Type, this.BaseURL, false);
            var that = this;
            // DEFINE METHOD TO HANDLE THE RESPONSE
            this.xmlHttp.onreadystatechange = function() {
                try {
                    // MOVE FORWARD IF TRANSACTION COMPLETE
                    alert(that.xmlHttp.readyState);
                    if (that.xmlHttp.readyState == 4) {
                        alert(that.xmlHttp.status);
                        // STATUS OF 200 INDICATES COMPLETED CORRECTLY
                        if (that.xmlHttp.status == 200) {
                            // WILL HOLD THE XML DOCUMENT
                            var xmldoc;
                            if (window.ActiveXObject) { // INTERNET EXPLORER
                                xmldoc = new ActiveXObject("Microsoft.XMLDOM");
                                xmldoc.async = "false";
                                that.Callback(that.xmlHttp.responseText);
                            }
                            else { // OTHER BROWSERS
                                //writeMessage("MakeRequest", that.xmlHttp.responseText);
                                that.Callback(that.xmlHttp.responseText);
                            }
                        }
                    }
                }
                catch (e)
                { alert(e) }
            }
            switch (this.Type) {
                case "GET":
                    //this.xmlHttp.setRequestHeader("Content-type", "application/json");
                    // MAKE CALL
                    this.xmlHttp.send(this.BaseURL);
                    break;
                case "POST":
                    this.xmlHttp.setRequestHeader("Content-type", "application/json");
                    this.xmlHttp.send(this.ParemeterString)
            }
        }
        else {
            // IF CONNECTION IS BUSY, WAIT AND RETRY
            setTimeout('GetAllAppsService', 5000);
        }
    } catch (e) {
        alert(e);
    }
}




通过创建AjaxRequest对象来调用ur wcf服务.
让我知道,如果它可以帮助您或对此有任何疑问.




what you need to call ur wcf service by making object of AjaxRequest.
Let me know , If it will help you or any query regarding this.


这篇关于通过AJAX消费WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆