【发布时间】:2014-02-05 12:16:37
【问题描述】:
请在下面找到 web 服务的代码。它是 post 方法 web 服务,但是当我试图从 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";
};
};
}
【问题讨论】:
-
它抛出了什么错误?
-
调试网络服务。是返回 Json 吗?如果是,则问题出在客户端。
-
在手机上启用调试器,看看是否能提供更多信息......browsers.about.com/od/allaboutwebbrowsers/ss/iphonedebugger.htm
-
我添加了我得到的错误。提前致谢。
-
我在 iPhone 应用程序中调用网络服务。它不是响应式网站。
标签: c# asp.net json wcf web-services