4
I did the following function:
$("#CategoryList").change(function () {
$("#SubCategoryDropDown").empty();
$.ajax({
dataType: "json",
url: '@Url.Action("GetSubCategory", "ProjectSubCategories")',
type: 'POST',
data: { id: $("#CategoryList").val() },
success: function (SubCategories) {
$("#SubCategoryDropDown").append('<option value="0">' + '--Selecione uma SubCategoria--' + '</option>');
$.each(SubCategories, function (i, SubCategory) {
$("#SubCategoryDropDown").append('<option value="' + SubCategory.Value + '">' + SubCategory.Text + '</option>');
});
},
error: function (ex) {
alert('Falha ao retornar Subcategoria.' + ex);
}
});
Controller:
public JsonResult GetSubCategory(int id)
{
var SubCategoryDropDown = new List<ProjectSubCategory>();
using (var context = new ApplicationDbContext())
{
SubCategoryDropDown = context.ProjectSubCategories.Where(x => x.CategoryId.Equals(id)).ToList();
}
return Json(new SelectList(SubCategoryDropDown, "SubCategoryId", "SubCategoryDescription"));
}
My problem is she always falls for the exception...
Can anyone help me? Thank you!
I have found that the problem is in the setRequestHeader mount on 'Xmlhttprequest. It is not working with HTTPS.
When I make the following code it works:
var http = new XMLHttpRequest();
var url = "https://localhost:44399/ProjectSubCategories/GetSubCategory";
var params = "id="+$("#CategoryList").val();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText)
ProjectSubCategories = JSON.parse(http.responseText);
$("#SubCategoryDropDown").append('<option value="0">' + '--Selecione uma Subcategoria--' + '</option>');
$.each(ProjectSubCategories, function (i,ProjectSubCategory) {
$("#SubCategoryDropDown").append('<option value="' + ProjectSubCategory.Value + '">' + ProjectSubCategory.Text + '</option>');
// here we are adding option for States
});
}
}
http.send(params);
Someone has a solution idea for using Json?
Instead of using Alert, print the error in the console using
console.log(ex)
then you will know what mistake you are making, supplement the question with that result, so that the community can help you better.– Tiago S
I added the error... I still don’t understand what’s wrong.
– Leonardo Giulianetti
From what I gave a search, I believe the error is because of the Selectlist, try to return the json of Subcategorydropdown and there in your view you access the properties directly
– Tiago S
One thing I noticed, if it’s standard, this method is only accepting GET, and you pass POST in ajax. Try to annotate the method with [Httppost] see if it helps. If not, try to change the type of ajax to 'GET'
– Grupo CDS Informática
I managed to fix... The error was 404, I changed the
dataType
forhtml
and it worked.– Leonardo Giulianetti
Hello friend, I believe that your date should be with the id between this way 'id'... so you can pull in your GET or POST... I could post your json, it would be easier to help...
– Diêgo Correia de Andrade