Jquery &引用;数据:JSON.stringify(e_data)";将数据值设置为null
WCF服务接口Jquery &引用;数据:JSON.stringify(e_data)";将数据值设置为null,jquery,ajax,wcf,Jquery,Ajax,Wcf,WCF服务接口 var e_Data={"userId":userId, "componentId":componentId, "eventId":eventId}; $.support.cors = true; $.ajax ({ type:'POST', data:JSON.stringify(e_Data), url:'http://localhost:11502/EventStreamService.svc/SentEventData', conten
var e_Data={"userId":userId, "componentId":componentId, "eventId":eventId};
$.support.cors = true;
$.ajax
({
type:'POST',
data:JSON.stringify(e_Data),
url:'http://localhost:11502/EventStreamService.svc/SentEventData',
contentType:'application/x-www-form-urlencoded; charset=UTF-8',
dataType:'json',
success: function(data){alert("data:"+data);}, //data:null ????why
error:function(){alert("not responding");}
});
WCF服务类
[OperationContract]
[WebInvoke( Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/SentEventData/")]
void SentEventData(int userId, string componentId, string eventId);
我已经创建了WCF服务,它从HTML制作的网站接收数据。ajax请求正在连接服务,但数据始终设置为null。我试图用alert(“data:+data”)测试数据的内容,结果是(data:null)。我的服务和jQueryAjax请求之间的通信正在工作,但由于空值,没有进一步的数据处理。非常感谢您的帮助。您对web服务的ajax只发送一个参数,即json字符串。在服务端,它将作为单个字符串而不是多个字符串接收。您需要将该字符串转换回单个值。感谢您的回复,我如何将JSON字符串更改为单个值?我将数据更改为一个对象,并删除了JSON.stringify,但在ajax中数据仍然为空值。你知道为什么会再次设置为空吗?我的服务也需要改变吗?
public void SentEventData(int userId, string componentId, string eventId)
{
evnd = new eventData();
evnd.UserId = userId;
evnd.ComponentId =componentId;
evnd.EventId = eventId;
try
{
evnd = this.ConnectToDatabase(evnd);
this.ConnectToEvent(evnd);
}
catch (Exception e) { throw e; }
}