【问题标题】:Make post webservices call with jquery through cross domain XML通过跨域 XML 使用 jquery 进行 post webservices 调用
【发布时间】:2012-09-30 02:26:29
【问题描述】:

我正在尝试从基于 ASP.NET 的 jQuery 移动网站调用 web 服务。 Web 服务采用 post 参数并返回 XML。 我无法更改 web 服务端。

我正面临跨域问题,据我了解,我需要使用代理。

我正在考虑使用通用处理程序,但我不知道该怎么做。

我的服务方法如下所示:

https://myserver.com/WCF/Info.svc/MyMehod1
https://myserver.com/WCF/Info.svc/MyMehod2

并取Post参数

在 c# 中实现这一点的好方法是什么?

谢谢

【问题讨论】:

    标签: c# web-services jquery-mobile cross-domain


    【解决方案1】:

    看看这个问题:Accessing web Service from jQuery - cross domain

    作为替代方案,您还可以创建一个使用 jQuery Ajax 调用的 HttpHandler。然后,处理程序可以调用 Web 服务并将输出写入 Http 响应。

    【讨论】:

    • 我使用了 httpHandler 替代方案并且它有效。一旦stackoverflow允许我,我将发布我的代码,对于那些将面临同样问题的人;)
    【解决方案2】:

    我终于让它工作了。

    对于那些有同样问题的人,

    在客户端,我使用了一个通用处理程序来执行 web 服务调用并公开结果。

    处理程序示例:

    public void ProcessRequest(HttpContext context)
    {
        string method = context.Request.QueryString["method"].ToString().ToLower();
    
        if (method == "MyMethod1")
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(CallWebService(method, param));
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
        else if (method == "MyMethod2")
        {
            context.Response.ContentType = "text/plain";
            string param = "myparam";
            context.Response.Write(CallWebService(method, param));
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
        else
        {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        }
    
    
    }
    
    private string CallWebService(string method, string param)
    {
        string ServeurURL = "https://myserver.com"; 
    
        System.Net.WebRequest req = System.Net.WebRequest.Create(ServeurURL + method);
        req.Method = "POST";
    
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(param);
        req.ContentType = "text/xml";
        req.ContentLength = byteArray.Length;
        System.IO.Stream reqstream = req.GetRequestStream();
        reqstream.Write(byteArray, 0, byteArray.Length);
        reqstream.Close();
    
        System.Net.WebResponse wResponse = req.GetResponse();
        reqstream = wResponse.GetResponseStream();
        System.IO.StreamReader reader = new System.IO.StreamReader(reqstream);
        string responseFromServer = reader.ReadToEnd();
    
        return responseFromServer;
    }
    

    jQuery/Ajax 调用:

    jQuery(function() {
            $('#btn1').click(function (e) {
                e.preventDefault();
                jQuery.ajax({
                    type: "GET",
                    url: "MyHandler.ashx",
                    data: "method=MyMethod1",
                    success: function (data) {
                        $('#display').html("<h1>" + data.toString() + "</h1>");
                    }
                });
            });
            $('#btn2').click(function (e) {
                e.preventDefault();
                jQuery.ajax({
                    type: "GET",
                    url: "MyHandler.ashx",
                    data: "method=MyMethod2",
                    success: function (data) {
                        $('#display').html("<h1>" + data.toString() + "</h1>");
                    }
                });
            });
        });
    

    现在它正在工作:)

    【讨论】:

    • 嘿@Distwo,很高兴这对你有用。提示:在这种情况下,用您选择的答案更新您的问题,而不是不接受答案,这可能是更好的礼仪。
    • 我想我可以将两者都标记为已接受回答,你是逻辑,我是相关代码,但它删除了第一个...... :(
    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2012-01-11
    • 2013-07-28
    相关资源
    最近更新 更多