2014-02-05 30 views
0

請查找下面的webservice代碼。這是post方法web服務,但是當我試圖從iPhone解析它。這是拋出錯誤。如何在ASP.NET C中的JSON中製作webservice#

function TestMe(strURI) { 
      //alert(strURI); 

      $.ajax({ 
       url: strURI, 
       type: 'POST', 
       //contentType: 'application/json', 
       data: JSON.stringify({ 
        "Input": [ 
         { 
          "MeetingId":"2", 
          "FromUserId":"1", 
          "DBMessage": "Test Message by index page" 
         }]       
       }), 
       success: function (result) { 
        alert('success'); 
        console.log(result); 
       }, 
       error: function (data) { 
        console.log(data); 
        alert('error'); 
       }, 
       complete: function() { 
        //alert('complete'); 
       } 
      }); 
     } 

在其他類我已經定義了這個方法

public string SaveDiscussionBoardMsg(Stream input) 
     { 
      string body = new StreamReader(input).ReadToEnd(); 
      string strJSONResult = ""; 
      objDAMeeting = new DA.Meeting(); 
      BO.BMO.DiscussionBoard objBODiscussionBoard = new BO.BMO.DiscussionBoard(); 

      //Method-2 
      //string JsonInsertSQL = @" {""Input"":[{""MeetingId"":1,""DBMessage"":""My Msg"",""FromUserId"":1},{""MeetingId"":1,""DBMessage"":""My Msg"",""FromUserId"":1}]}"; 
      //string JsonInsertSQL = @" {""Input"":[{""MeetingId"":1,""DBMessage"":""My Msg"",""FromUserId"":1}]}"; 
      dynamic dynObj = Newtonsoft.Json.JsonConvert.DeserializeObject(body); 
      foreach (var item in dynObj.Input) 
      { 
       objBODiscussionBoard.MeetingId = Convert.ToInt32(item.MeetingId.ToString()); 
       objBODiscussionBoard.DBMessage = item.DBMessage.ToString(); 
       objBODiscussionBoard.FromUserId = Convert.ToInt32(item.FromUserId.ToString()); 

       strJSONResult = objDAMeeting.SaveDiscussionBoardMsg(objBODiscussionBoard); 
      } 

      strJSONResult = string.Format("{0}{1}{2}", strPrefix, "\"" + strJSONResult + "\"", strPostfix); 
      //HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; 
      //HttpContext.Current.Response.Write(strJSONResult); 

      objDAMeeting = null; 

      return strJSONResult; 
     } 

我們正在使用WCF

[OperationContract] 
     [WebInvoke(Method = "POST", 
      ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "/SaveDiscussionBoardMsg")] 
     string SaveDiscussionBoardMsg(Stream input); 

我得到這個錯誤。請找到下面的錯誤。後

{ 
    html =  { 
     body =   { 
      div =    { 
       "div#@[email protected]#" =     { 
        id = content; 
       }; 
       p =     (
        "Request Error", 
             { 
         a = "service help page"; 
         "p#@[email protected]#" =       { 
          xmlns = ""; 
         }; 
        } 
       ); 
      }; 
     }; 
     head =   { 
      style = "BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}"; 
      title = "Request Error"; 
     }; 
     "html#@[email protected]#" =   { 
      xmlns = "http://www.w3.org/1999/xhtml"; 
     }; 
    }; 
} 
+4

它拋出什麼樣的錯誤? –

+0

調試Web服務。它是否返回Json?如果是這樣,問題是客戶端。 –

+0

啓用調試器的手機,看看是否給你任何更多的信息.​​... http://browsers.about.com/od/allaboutwebbrowsers/ss/iphonedebugger.htm – hutchonoid

回答

0

嘗試增加數據類型: 'JSON' 作爲

$.ajax({ 
      url: strURI, 
      type: 'POST', 
      dataType: 'json', 
      //contentType: 'application/json', 
      data: JSON.stringify({ 
       "Input": [ 
        { 
         "MeetingId":"2", 
         "FromUserId":"1", 
         "DBMessage": "Test Message by index page" 
        }]       
      }), 
      success: function (result) { 
       alert('success'); 
       console.log(result); 
      }, 
      error: function (data) { 
       console.log(data); 
       alert('error'); 
      }, 
      complete: function() { 
       //alert('complete'); 
      } 
     }); 
相關問題