簡體   English   中英

如何在ASP.NET C#中使用JSON創建Web服務

[英]How to make webservice in JSON in ASP.NET C#

請找到以下用於網絡服務的代碼。 它是post方法的webservice,但是當我嘗試從iPhone解析它時。 它拋出錯誤。

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#@Attributes@#" =                 {
                    id = content;
                };
                p =                 (
                    "Request Error",
                                        {
                        a = "service help page";
                        "p#@Attributes@#" =                         {
                            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#@Attributes@#" =         {
            xmlns = "http://www.w3.org/1999/xhtml";
        };
    };
}

將dataType:'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');
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM