【发布时间】:2019-07-05 23:25:11
【问题描述】:
我按照可能重复的问题中的步骤操作,但它没有解决我的错误。
我的整个应用程序在上传到服务器时都能正常工作,除了一个按钮。
按钮是一个带有 ID 的简单按钮。当我点击它时,一个函数会捕捉到点击,从用户输入字段中抓取相关数据,将其发送到控制器,然后将数据保存到数据库中。
当我上传到服务器时,网站上的所有其他内容都运行良好。但是,在尝试查找控制器操作时,该按钮会引发 404 错误。
这是按钮:
<input type="button" id="btnAdd" value="Add" class="btn btn-primary" />
调用控制器动作的脚本:
$(function () {
$("#btnAdd").click(function () {
//snipped for brevity
//Send the records to server for saving to database.
$.ajax({
type: "POST",
url: "/Designees/InsertDesg",
data: '{FnameD: "' + txtFnameD.val() + '",LnameD: "' + txtLnameD.val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
$("#txtLandownerID").val(r.LandownerID);
$("#txtPermitNo").val(r.PermitNo);
$("#txtExpYear").val(r.ExpYear);
//Add the Name value to first cell.
SetValue(row, 2, txtFnameD);
SetValue(row, 3, txtLnameD);
//Add the row to the WebGrid.
webGrid.append(row);
window.location.href = r.Url;
}
});
});
控制器动作:
[HttpPost]
public JsonResult InsertDesg(Designee designee)
{
designee.CreatedDate = System.DateTime.Now;
designee.ModifiedDate = System.DateTime.Now;
var user = context.Users.SingleOrDefault(u => u.BhCode == User.Identity.Name);
designee.EnteredBy = user.Id;
db.Designees.Add(designee);
db.SaveChanges();
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Designees", new { id = designee.LandownerID, permit = designee.PermitNo, year = designee.ExpYear });
return Json(new { Url = redirectUrl, designee });
}
我希望服务器上发生的事情与我的本地系统上发生的事情相同。谁能想到解决方案?我的 IIS 日志只显示 404 错误。
解决方案
我不知道为什么会这样修复它,但是我将函数调用从上面更改为下面,现在它可以正常工作了:
var RootUrl = '@Url.Content("~/")';
//Send the records to server for saving to database.
$.ajax({
type: "POST",
url: RootUrl + "Designees/InsertDesg",
【问题讨论】:
-
你可以尝试在 url 中给出完整路径而不是这个 url:
"/Designees/InsertDesg"到http://your_path/Designees/InsertDesg -
@NoumanZakir 您链接的帖子无法解决我的问题;我已经看过了。
-
@Carthax 或者只尝试不带
/` "Designees/InsertDesg" 的 URL ` -
尝试使用“../Designees/InsertDesg”或“~/Designees/InsertDesg”或“./Designees/InsertDesg”代替您的网址。根据服务器的设置方式,这可能会有所帮助。
标签: c# asp.net-mvc