javascript - $.getJSON() 没有被触发
问题描述
出于某种奇怪的原因,我的 .getJSON() 函数不起作用。它也没有给我任何错误消息。
我的 JavaScript 代码:
$(document).ready(function () {
console.log("Hello from riskanalysis.delete.js");
var categoryTd = $('#categoryId');
var categoryText = categoryTd.text();
var categoryInt = parseInt(categoryText);
console.log(categoryInt);
console.log(categoryText);
console.log("Hello before");
$.getJSON("/riskanalysis/getCategoryNameById?categoryId=" + categoryInt)
.done(function (categoryName) {
// On success
console.log("Category name is: " + categoryName);
categoryTd.text(categoryName);
}).fail(function (e) { console.log(e); });
console.log("Hello");
console.log("Hello after");
});
我的控制器代码:
[Route("riskanalysis/getCategoryNameById")]
[HttpGet]
public string GetCategoryNameById(int categoryId)
{
return _manager.GetCategoryNameById(categoryId);
}
我的经理代码:
public string GetCategoryNameById(int categoryId)
{
using (var dbContext = new Entities.DanoneRiskanalysisContext())
{
Entities.Category entitiy = dbContext.Category
.Where(c => c.Id.Equals(categoryId))
.FirstOrDefault();
if (entitiy != null)
{
return entitiy.ListCategories;
}
}
return null;
}
我的经理界面代码:
string GetCategoryNameById(int categoryId);
我的 .cshtml 代码:
<tr>
<td>Category</td>
<td id="categoryId">@Html.DisplayFor(model => model.categoryId)</td>
</tr>
添加了 .fail 函数,结果如下:
添加.fail(function( jqxhr, textStatus, error ) { var err = textStatus + ", " + error; console.log( "Request Failed: " + err ); });
结果:请求失败:解析器错误,语法错误:JSON 中位置 0 的意外标记 T
有谁知道为什么这个功能不起作用?
已经谢谢了!
解决方案
更改$.getJSON()
为$.ajax
解决了我的问题!
推荐阅读
- cmake - cmake 命令有什么方法可以在文件中使用参数?
- java - RabbitMQ 没有在 Spring Boot 中自动配置
- c++ - C++ 运算符重载中的类型推断
- node.js - 为什么我的 deleteMessage 功能不起作用?
- angular - PrimeNG:资产文件夹中的 MenuItem 图标未显示
- c++ - 如何以与 C++ 中的父字符串相同的顺序获取字符串的字符集
- gnu-make - 如何检测是否需要共享库符号链接?
- android - 创建 Uri 到文件,有点无法提交数据
- php - 如何通过条件使用“with”laravel 关系函数来获取数据?
- matplotlib - Matplotlib 的后端不会从 TkAgg 更改为 GR