По какой-то странной причине моя функция .get JSON () не работает. Это также не дает мне никаких сообщений об ошибках.
Мой 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);
Мой код .cs html:
<tr>
<td>Category</td>
<td id="categoryId">@Html.DisplayFor(model => model.categoryId)</td>
</tr>
Добавлена функция .fail, это результат:
![enter image description here](https://i.stack.imgur.com/KX0Vc.png)
Добавлено .fail(function( jqxhr, textStatus, error ) { var err = textStatus + ", " + error; console.log( "Request Failed: " + err ); });
Результат: Запрос не выполнен: parsererror, SyntaxError: Неожиданный токен T в JSON в позиции 0
У кого-нибудь есть идеи, почему эта функция не работает?
Спасибо уже!