我已经尝试了每种组合来发送请求,以从 jQuery 向 RESTful WCF 发送 POST 请求。
有人可以模仿并使其发挥作用吗?代码在这里:http://pastebin.com/Ua97919C
过去两年我一直在使用 WCF,但是每次我发送 POST 请求时都会让我很挣扎。
在过去 4 天里,我一直在努力让它发挥作用,并且浏览了至少 35-40 个帖子。
最终,此请求将从 iPhone 发送到 WCF。
当我用 Fiddler 检查时,错误主要是:*服务器在处理请求时遇到错误。异常消息是“传入消息具有意外的消息格式‘Raw’”。该操作的预期消息格式为“Xml”、“Json”。这可能是因为尚未在绑定(bind)上配置 WebContentTypeMapper。有关更多详细信息,请参阅 WebContentTypeMapper 的文档。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是: 在
System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
最佳答案
在您的解决方案中添加一个 Global.ascx
文件,并将代码替换为以下内容
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
还有一件事dataType:'text'
$.ajax({
type: "POST",
url: "http://localhost:4638/Edulink.svc/SaveUserData",
dataType: "text",
contentType: "application/json",
data:'{"EmailID":"praveen", "LevelID": 1}',
success:function(data, status) {
console.log(data); //gives 1
},
error:function(request, status, error) {
alert("o0ops");
}
});
关于jquery - 使用 json 向 RESTful WCF 发送 Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537634/