2011-12-16 57 views
7

我已經嘗試過每個組合都發送一個請求,以從jQuery向RESTful WCF發送POST請求。使用json發送Post請求到RESTful WCF

有人可以模仿並使其工作。代碼在這裏:http://pastebin.com/Ua97919C

我與過去2年的WCF一起工作,但每次發送POST請求都會讓我苦不堪言。

我努力使它從過去4天開始工作,並經歷了至少35-40個帖子。

最終,這個請求將從iPhone發送到WCF。

當我用Fiddler檢查時,錯誤主要是:*服務器在處理請求時遇到錯誤。異常消息是'傳入消息具有意外的消息格式'原始'。預期的操作消息格式是'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) 

回答

4

添加Global.ascx文件YOUE解決方案,替換代碼與以下

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'

$.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");   
    } 
}); 
+0

否..然後在jQuery.min.js上拋出錯誤。我早些時候也嘗試過。 – iMatoria 2011-12-16 17:22:35

+0

我已嘗試/重試兩個。 – iMatoria 2011-12-16 17:31:03

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,在這種情況下,你原來的請求是正確的。