从jquery将数据发送到wcf Webservice



我正在尝试使用 jquery 从 html 页面将 json 字符串发送到 wcf 网络服务。但它给出了错误并且没有返回所需的结果。html 页面显示错误。如何调试服务以查看页面是否实际调用 Web 服务。请帮忙。

IService1.cs

public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

服务1.svc.cs

namespace WcfService2
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}

j查询代码

var markers = [{ "value": "128" }];
$.ajax({
type: "POST",
async: false,
data: JSON.stringify({ Markers: markers }),
url: "http://localhost:13008/Service1.svc/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",          
success: function (data) {
alert(data);
},
error: function () {
alert("Error");
}
});

我认为你需要提到webHttpBinding来配置Restful服务的端点。默认情况下,当您在 Visual Studio 中启动服务时,它可能使用 basicHttpBinding。

您还需要添加属性[WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json,uriTemplate="/json/{id}")]未在 ide 中签入。但是您需要添加与之类似的内容。

删除 AJAX 调用中的数据部分,并在 url 中传递参数。喜欢这个: http://localhost:13008/service1.svc/GetData?value=128

最新更新