0
在我的應用程序中,我有一個編輯配置文件頁面,您可以在下面看到;Ajax調用導致一個奇怪的錯誤
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master"
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.EmployeeViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Edit", "Employee"))
{%>
<%: Html.AntiForgeryToken()%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>Edit Employee</legend>
<% if (TempData["Message"] != null && TempData["Message"].ToString().Length > 0)
{ %>
<p class="success">
At <% Response.Write(DateTime.Now.ToString("T")); %>
you entered <%: TempData["Message"]%>.
</p>
<%} %>
<table class="groupBorder" id="editTable">
<tr>
<td style="text-align: right;"><%: Html.LabelFor(model => model.Employee.CompanyId)%></td>
<td>
<%: Html.DropDownListFor(model => model.Employee.CompanyId, Model.CompaniesSelectList)%>
</td>
</tr>
<tr>
<td style="text-align: right;">
<%: Html.LabelFor(model => model.Employee.EmployeeNumber)%>
</td>
<td>
<%: Html.TextBoxFor(model => model.Employee.EmployeeNumber,
new { maxlength = "4", style = "width:35px;text-transform: uppercase;" })%>
<%: Html.ValidationMessageFor(model => model.Employee.EmployeeNumber)%>
<span class="error" id="errorMessage">
<% if (TempData["ErrorMessage"] != null)
{ %>
<%: TempData["ErrorMessage"].ToString()%>
<% } %>
</span>
</td>
</tr>
<tr>
<td style="text-align: center;padding-top:20px;" colspan="2">
<input type="submit" value="Save" id="btnSubmit"/></td>
</tr>
</table>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "List")%>
</div>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#Employee_CompanyId").change(function() {
validateEmployeeNumber();
});
$("#Employee_EmployeeNumber").change(function() {
validateEmployeeNumber();
});
});
function validateEmployeeNumber() {
var employeeNumber = $("#Employee_EmployeeNumber").val().toUpperCase();
$("#Employee_EmployeeNumber").val(employeeNumber);
if (employeeNumber.length == 0) {
$('#errorMessage').text("Employee Number Required");
}
if (employeeNumber.length > 0) {
$('#errorMessage').text('');
$('#btnSubmit').attr("disabled", false);
var companyId = $("#Employee_CompanyId").val();
var employeeId = $("#Employee_EmployeeId").val();
$.ajax({
data: { companyId: companyId, employeeNumber: employeeNumber, employeeId: employeeId },
dataType: "text",
success: function (data) {
if (data.length > 0) {
$('#errorMessage').text(data);
$('#btnSubmit').attr("disabled", true);
}
},
type: "POST",
url: '<%= Url.Action("ValidateEmployeeNumber", "Employee")%>'
});
}
}
</script>
</asp:Content>
這裏使用的控制器如下;
[HttpPost]
[Authorize(Roles = "Administrator, ManagerAccounts, ManagerIT")]
public string ValidateEmployeeNumber(string companyId, string employeeNumber, string employeeId = null)
{
var cid = 0;
var flag = int.TryParse(companyId, out cid);
if (!flag)
{
return string.Empty;
}
var eid = 0;
if (employeeId != null)
{
flag = int.TryParse(employeeId, out eid);
if (!flag)
{
return string.Empty;
}
}
if (Employee.ValidateEmployeeNumberFormat(employeeNumber) == false)
{
return "Invalid format";
}
var employeeNumberExists = Employee.GetEmployeeByEmployeeNumber(cid, employeeNumber, eid);
return employeeNumberExists ? "Invalid Employee Number already in use." : string.Empty;
}
本頁面本身已被編輯,以顯示我感興趣的是位。 的頁面是爲了使管理員能夠編輯僱員的個人資料,包括員工編號。 對服務器的Ajax調用是檢查員工編號是否唯一。 現在在我的開發人員機器上,它可以在所有服務器上運行;開發,測試和生產。但對於一些在其他機器上工作的人來說,這是行不通的。當他們嘗試編輯生產服務器上的員工編號時,他們突然在AJAX調用之後立即出現大量javascript文本。 爲什麼會發生這種情況,如何解決這個問題?
紅色文字說什麼? –
紅色文本是一個看起來像縮小的JavaScript流。我不知道它來自哪裏。 – arame3333
你可以發佈控制器代碼,以便我們可以看到你傳回的東西嗎? –