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

时间:2011-12-16 17:07:05

标签: jquery iphone asp.net wcf wcf-rest

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

有人可以模仿并使其发挥作用。代码在这里:http://pastebin.com/Ua97919C

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

我正在努力让它在过去的4天里工作,并且经历了至少35-40个帖子。

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

当我用Fiddler检查时,错误主要是:*服务器在处理请求时遇到错误。异常消息是'传入消息具有意外的消息格式'Raw'。操作的预期消息格式是'Xml','Json'。这可能是因为尚未在绑定上配置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)

2 个答案:

答案 0 :(得分:4)

在解决方案中添加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");           
    }
});

答案 1 :(得分:2)

问题是手术的体型。你声明它是

[WebInvoke(
        Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "/SaveUserData")]
string SaveUserData(UserInfo userInfo);

意味着请求必须在对象中包装,并且具有对象名称的成员。如果您在下面发送此请求,它应该可以正常工作。

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: url,
    data: '{"userInfo":{"EmailID":"praveen", "LevelID": 1}}',
    dataType: "json",
    processData: false,
    success: function (data, textStatus, jqXHR) {
        debugger;
    },
    error: function (jqXHR, textStatus, errorThrown) {
        debugger;
    }
});

另一种方法是将操作的BodyStyle属性更改为Bare,在这种情况下,您的原始请求是正确的。

相关问题