带有Json的jQuery Ajax调用WCF服务传递到WCF服务 [英] jQuery Ajax with Json call to WCF service pass-through to WCF service

查看:114
本文介绍了带有Json的jQuery Ajax调用WCF服务传递到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Json使用jQuery Ajax调用来调用Web服务,该Web服务是传递功能,并调用另一个Web服务.如果我不使用ajax,则此方法有效,可直接用作后端服务的ajax调用,但不可用作传递服务的ajax调用.我的问题是如何使Ajax通过Web服务传递到Web服务才能工作?

I'm attempting to use jQuery Ajax call with Json to call a webservice which is a pass-through and calls another webservice. This works if I don't use ajax, works as an ajax call to the backend service directly, but does not work as ajax call to pass-through service. My question is how do I get an ajax to pass-through webservice to webservice to work?

这是我到目前为止的代码:

This is the code I have so far:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string GetData(int value);
    }

网络服务背后:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

直通服务:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service2 : IService1
{
    public string GetData(int value)
    {
       var service = new ServiceReference1.Service1Client("clientContractStuff");
       var testString = service.GetData(value);
       return testString;
    }
}

后面的页面代码:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    protected void CodeBehindWcf(object sender, EventArgs e)
    {
        var service = new ServiceReference1.Service1Client("clientContractStuff");
        var testString = service.GetData(5);
        TextBox1.Text = testString;
    }

    protected void CodeBehindWcfUsingService2(object sender, EventArgs e)
    {
        var service = new Service2();
        var testString = service.GetData(5);
        TextBox2.Text = testString;
    }
}

JavaScript和HTML:

Javascript and Html:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
    Inherits="WcfService1.WebUserControl1" %>
<script type="text/javascript" src="Scripts/jquery-1.4.4-vsdoc.js"></script>
<script type="text/javascript" src="Scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
    function ajaxService2() {
        serviceUrl = "Service2.svc/GetData";
        $.ajax({
            type: "POST",
            url: serviceUrl,
            data: "{\"value\":\"1\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (transport) {
                var string = transport;
                $("#Text2").val(string);
            }
        });
    }

    function ajaxService1() {
        serviceUrl = "Service1.svc/GetData";
        $.ajax({
            type: "POST",
            url: serviceUrl,
            data: "{\"value\":\"2\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (transport) {
                var string = transport;
                $("#Text1").val(string);
            }
        });
    }
</script>
<p>
    <input id="Button1" type="button" value="Ajax call service 1" onclick="ajaxService1()" />
    <input id="Text1" name="Text1" type="text" /></p>
<p>
    <input id="Button2" type="button" value="Ajax call service 2" onclick="ajaxService2()" />
    <input id="Text2" name="Text2" type="text" /></p>
<p>
    <asp:Button ID="Button3" runat="server" Text="Code Behind" OnClick="CodeBehindWcf" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
<p>
    <asp:Button ID="Button4" runat="server" Text="Code Behind Use Service 2" OnClick="CodeBehindWcfUsingService2" />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></p>

除Button 2和ajaxService2函数之外,所有其他功能均可用.这将引发错误:远程服务器返回了意外的响应:(400)错误的请求."

Everything works except Button 2 and ajaxService2 function. This throws an error: 'The remote server returned an unexpected response: (400) Bad Request.'

推荐答案

为了使用Ajax,必须将webhttpbinding用作直通式Web服务的端点,并且我假定也将其用作后端服务.

In order to use Ajax, webhttpbinding must be used for the endpoint of the pass-through webservice and I assumed would also be used for the backend service.

在"Service References \ Reference.cs"中查看自动生成的服务参考代码:

Looking at the autogenerated service reference code in 'Service References\Reference.cs':

public interface IService1 {

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
    string GetData(int value);
}

很容易看出它是在SOAP而不是REST下生成代码的.由于webhttpbinding是REST绑定,因此它将不起作用.我将后端服务更改为使用basichttpbinding,并且一切正常.

It's easy to see it's generating code under SOAP and not REST. Since webhttpbinding is a REST binding it will not work. I changed the backend service to use basichttpbinding and everything works as it should.

这篇关于带有Json的jQuery Ajax调用WCF服务传递到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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