json返回的结果是 html 而不是 json
jsonresult is returning html not json
我向 Godaddy 发布了一个 ASP.NET MVC 应用程序。我遇到了一个 Ajax 调用的问题,该调用应该 return 一个 JSON 反对它 return 我网站索引页的 HTML。我以前遇到过应用程序菜单栏链接的问题,它们会重定向到我网站的主页。我能够通过向我的站点的 web.config 添加规则来解决问题,该规则排除了包含该应用程序的子文件夹:<add input="{REQUEST_URI}" pattern="^/(codesnippetapp)" negate="true" />
我检查了 Chrome 中的开发控制台和请求 URL 是错的。 URL 应该是 http://www.mattdailey.net/codesnippetapp/Home/GetCodeData instead it is http://www.mattdailey.net/Home/GetCodeData
这是 Ajax 调用和检索 JSON:
的 JsonResult 函数
$.ajax({
url: '/Home/GetCodeData',
type: 'Post',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(selectedSnippetID),
success: function (data) {
if (data.success) {
$("#snippetcode").val(data.snippetCode);
} else {
alert('invalid ID' + data.success);
}
}
});
[HttpPost]
public JsonResult GetCodeData(int snippetID)
{
CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
{
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode });
}
return Json(new { success = false });
}
我需要向我的应用 web.config 添加什么?或者我是否需要将代码添加到我网站的 web.config?
更新:
我尝试使用 GET 方法,但出现内部服务器错误。我使用 razor @section 代码将脚本从外部文件移动到视图本身:
@section Scripts
{
... jQuery code
}
然后将此添加到 _Layout.cshtml:
@RenderSection("Scripts", required: false)
我确实将剃刀@Url.Action 助手添加到 Ajax url。我还更改了将应用程序发布到 Godaddy 的方式,我认为这也有所帮助。我从 FTP 方法更改为文件系统。然后我通过 FTP 手动上传了文件。现在可以使用了。
感谢大家的帮助。我写下了我的步骤,希望这能帮助处于类似情况的其他人。
/
开头的 url 值将使它成为您网站的根目录(不是您的应用)。
使用 Url.Action
辅助方法生成您的操作方法的路径。
url: '@Url.Action("GetCodeData","Home")',
如果您的 javascript 在剃须刀视图内,这应该有效。如果您的代码在外部 js 文件中,请在 razor 视图中调用此方法并将其分配给变量并在您的 js 文件中使用它,如
第二部分所述
使用JsonRequestBehavior.AllowGet
public JsonResult GetCodeData(int snippetID)
{
CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
{
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode },JsonRequestBehavior.AllowGet);
}
return Json(new { success = false },JsonRequestBehavior.AllowGet);
}
这应该是一个 GET 操作,因为您正在尝试 return Json 到客户端。
$.ajax({
url: '/Home/GetCodeData',
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(selectedSnippetID),
success: function (data) {
if (data.success) {
$("#snippetcode").val(data.snippetCode);
} else {
alert('invalid ID' + data.success);
}
}
});
public JsonResult GetCodeData(int snippetID)
{
CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
{
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode }, JsonRequestBehavior.AllowGet);
}
return Json(new { success = false }, JsonRequestBehavior.AllowGet);
}
我向 Godaddy 发布了一个 ASP.NET MVC 应用程序。我遇到了一个 Ajax 调用的问题,该调用应该 return 一个 JSON 反对它 return 我网站索引页的 HTML。我以前遇到过应用程序菜单栏链接的问题,它们会重定向到我网站的主页。我能够通过向我的站点的 web.config 添加规则来解决问题,该规则排除了包含该应用程序的子文件夹:<add input="{REQUEST_URI}" pattern="^/(codesnippetapp)" negate="true" />
我检查了 Chrome 中的开发控制台和请求 URL 是错的。 URL 应该是 http://www.mattdailey.net/codesnippetapp/Home/GetCodeData instead it is http://www.mattdailey.net/Home/GetCodeData
这是 Ajax 调用和检索 JSON:
的 JsonResult 函数$.ajax({
url: '/Home/GetCodeData',
type: 'Post',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(selectedSnippetID),
success: function (data) {
if (data.success) {
$("#snippetcode").val(data.snippetCode);
} else {
alert('invalid ID' + data.success);
}
}
});
[HttpPost]
public JsonResult GetCodeData(int snippetID)
{
CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
{
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode });
}
return Json(new { success = false });
}
我需要向我的应用 web.config 添加什么?或者我是否需要将代码添加到我网站的 web.config?
更新: 我尝试使用 GET 方法,但出现内部服务器错误。我使用 razor @section 代码将脚本从外部文件移动到视图本身:
@section Scripts
{
... jQuery code
}
然后将此添加到 _Layout.cshtml:
@RenderSection("Scripts", required: false)
我确实将剃刀@Url.Action 助手添加到 Ajax url。我还更改了将应用程序发布到 Godaddy 的方式,我认为这也有所帮助。我从 FTP 方法更改为文件系统。然后我通过 FTP 手动上传了文件。现在可以使用了。
感谢大家的帮助。我写下了我的步骤,希望这能帮助处于类似情况的其他人。
/
开头的 url 值将使它成为您网站的根目录(不是您的应用)。
使用 Url.Action
辅助方法生成您的操作方法的路径。
url: '@Url.Action("GetCodeData","Home")',
如果您的 javascript 在剃须刀视图内,这应该有效。如果您的代码在外部 js 文件中,请在 razor 视图中调用此方法并将其分配给变量并在您的 js 文件中使用它,如
使用JsonRequestBehavior.AllowGet
public JsonResult GetCodeData(int snippetID)
{
CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
{
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode },JsonRequestBehavior.AllowGet);
}
return Json(new { success = false },JsonRequestBehavior.AllowGet);
}
这应该是一个 GET 操作,因为您正在尝试 return Json 到客户端。
$.ajax({
url: '/Home/GetCodeData',
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(selectedSnippetID),
success: function (data) {
if (data.success) {
$("#snippetcode").val(data.snippetCode);
} else {
alert('invalid ID' + data.success);
}
}
});
public JsonResult GetCodeData(int snippetID)
{
CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
{
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode }, JsonRequestBehavior.AllowGet);
}
return Json(new { success = false }, JsonRequestBehavior.AllowGet);
}