我正在嘗試使用JQuery與Json調用Ajax調用一個Web服務,這是一個傳遞並調用另一個Web服務。如果我不使用ajax,這將起作用,直接作爲對後端服務的ajax調用,但不作爲ajax調用傳遞服務。我的問題是如何獲得一個Ajax通過web服務傳遞到webservice工作?jQuery Ajax與Json調用WCF服務傳遞到WCF服務
這是我的代碼至今:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string GetData(int value);
}
web服務背後:
[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:
<%@ 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>
一切正常,除了按鍵2和ajaxService2功能。這會引發錯誤:'遠程服務器返回了意外的響應:(400)錯誤的請求。'
順便說一句,它是「jQuery」。 – 2011-04-21 00:19:25