使用json将发送请求发送到RESTful WCF

编程入门 行业动态 更新时间:2024-10-27 17:22:03
本文介绍了使用json将发送请求发送到RESTful WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经尝试过每个组合来发送一个从jQuery向RESTful WCF发送POST请求的请求。

I have tried every combination to send a request to send a POST request to RESTful WCF from jQuery.

有人可以模仿并使其正常运作。代码在这里: pastebin/Ua97919C

Can someone imitate and make it working. Code is here : pastebin/Ua97919C

我在过去2年与WCF合作,但每次发送POST请求都会让我感到很挣扎。

I am working with WCF from past 2 years, but every time i send a POST request it makes me struggle a lot.

我正努力让它发挥作用从过去的4天开始,经历了至少35-40个帖子。

I am struggling to make it work from past 4 days and gone through atleast 35-40 posts.

最终,此请求将从iPhone发送到WCF。

Eventually, this request would be sent from iPhone to WCF.

当我用Fiddler检查时,错误主要是:*服务器遇到处理请求的错误。异常消息是'传入消息具有意外的消息格式'Raw'。该操作的预期消息格式是'Xml','Json'。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关更多详细信息,请参阅WebContentTypeMapper的文档。请参阅服务器日志以获取更多详异常堆栈跟踪是: at

When i check it with Fiddler the error mostly is: *The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: at

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 文件并将代码替换为以下

Add a Global.ascx file in youe solution and replace the code with following

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(); } }

还有一件事chnage dataType :'text'

one more thing chnage dataType:'text'

$.ajax({ type: "POST", url: "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"); } });

更多推荐

使用json将发送请求发送到RESTful WCF

本文发布于:2023-10-11 22:16:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   json   WCF   RESTful

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!