繁体   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