【发布时间】:2018-08-11 00:04:38
【问题描述】:
我正在尝试使用 jquery 从 html 页面向 wcf webservice 发送 json 字符串。但它给出了错误并且没有返回所需的结果。 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; }
}
}
Service1.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;
}
}
}
jquery 代码
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");
}
});
【问题讨论】:
-
您的网络服务似乎没有配置为使用或发送 JSON,请尝试删除
contentType和dataType并将data字段设置为{ "value": "128" }。跨度> -
@Musa 不,它仍然给出错误。
-
var markers = [{ "value": 128 }];怎么样 -
@LeonardoSeccia 不工作
-
你有没有在端点配置中提到 webhttpbinding