MVC前台得不到后台传递过来的JSON结果如何解决?前台究竟如何调试?
问题:
1.UserProfile已经在WCF里序列化好了,在MVC前台与控制器之间还要再次序列化成JSON吗?
2.目前总是只能返回状态码200,我用FIREBUG看了一下网络跟踪,前面好像还有个302 found,其它状态码基本都是200?前台究竟如何设置才能像后台一样可以一步一步调试?网上找很久,也好像只是说了一下工具,没有很具体的实用的步骤。
UserProfile的类是WCF服务里定义:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/SmartQIS.Library.Models")]
public partial class UserProfile
{
private string m_DatabaseField;
private System.DateTime m_LastDateField;
private string m_LastWeekField;
private bool m_MultiDBField;
private string m_PageSizeField;
private string m_PasswordField;
private string m_UserField;
private string m_UserLevelField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)]
public string m_Database
{
get
{
return this.m_DatabaseField;
}
set
{
this.m_DatabaseField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public System.DateTime m_LastDate
{
get
{
return this.m_LastDateField;
}
set
{
this.m_LastDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)]
public string m_LastWeek
{
get
{
return this.m_LastWeekField;
}
set
{
this.m_LastWeekField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=3)]
public bool m_MultiDB
{
get
{
return this.m_MultiDBField;
}
set
{
this.m_MultiDBField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=4)]
public string m_PageSize
{
get
{
return this.m_PageSizeField;
}
set
{
this.m_PageSizeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=5)]
public string m_Password
{
get
{
return this.m_PasswordField;
}
set
{
this.m_PasswordField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=6)]
public string m_User
{
get
{
return this.m_UserField;
}
set
{
this.m_UserField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=7)]
public string m_UserLevel
{
get
{
return this.m_UserLevelField;
}
set
{
this.m_UserLevelField = value;
}
}
}
控制器:
public JsonResult GetDefaultDB(string strUserName)
{
WCFServiceClient client = new WCFServiceClient();
wv = new WebVariable();
//wv.m_ServerIP = @"DESKTOP-L2KUIQN\SQLEXPRESS";
wv.m_ServerIP = "QISDB01";
wv.m_DbName = "BOMDB";
wv.m_UserID = strUserName;
userprofile = client.GetLoginDefault(wv);
client.Close();
return Json(userprofile,JsonRequestBehavior.AllowGet);
}
控制器这里我改了几次:将userprofle转成JSON字串格式,前台还是进不了success,一直进入error. status返回码200.
用火狐的FIREBUG我也跟踪不到究竟从后台返回了什么字串,但在后台调试测试GetDefaultDB时,能看JSON的data是UserProfile的数据,前台怎么改都拿不到这个数据。
前台代码,JS的主要是红色标示这里,改成$.ajax格式也一样,拿不到后台传过来的Data:
@model SmartQIS.MVC.Models.LogOnModel
@{
ViewBag.Title = "Log on.";
}
<hgroup class="title">
<h1>Log On.</h1><br/>
<!--<h6>Enter username and password below.</h6>-->
</hgroup>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm((string)ViewBag.FormAction, "Account")) {
<fieldset>
<legend>Log On Form</legend>
<ol>
<li>
<script type="text/javascript">
$(document).ready(function ()
{
//alert("test");
$("#UserName").change(function ()
{
var username = $("#UserName").val();
if (username != "")
{
//alert(username);
var url = '@Url.Action("GetDefaultDB", "Account")';
$.getJSON(url, { "strUserName": username },function (data)
{
alert("testing");
$('select#DefaultDB option:selected').text(username);
});
}
});
});
</script>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.DefaultDB)
@Html.DropDownListFor(m => m.DefaultDB, ViewData["DefaultDBList"] as IEnumerable<SelectListItem>, new {style="font-size:10pt;width:258px;height:31px;padding: 2px 24px 0 8px"})
</li>
<br/>
<li>
@Html.LabelFor(m => m.AppServer)
@Html.DropDownListFor(m => m.AppServer, ViewData["AppServerList"] as IEnumerable<SelectListItem>, new {style="width:258px;height:31px;padding: 0 24px 0 8px"})
</li>
<br/>
<br/>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new {@class = "checkbox" })
</li>
</ol>
<br/>
<input type="submit" value="Log On" title="Log On" />
</fieldset>
}