问题
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);
}
Behind webservice:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
Pass-through service:
[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;
}
}
Page code behind:
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 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>
Everything works except Button 2 and ajaxService2 function. This throws an error: 'The remote server returned an unexpected response: (400) Bad Request.'
回答1:
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.
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);
}
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.
回答2:
In Service2.GetData
you initialize client of your Service2
which calls Service2.GetData
again.
来源:https://stackoverflow.com/questions/5738097/jquery-ajax-with-json-call-to-wcf-service-pass-through-to-wcf-service