开发者

$.getJSON doesn't seem to work

I've been looking at this issue nearly all day and I have no ide开发者_如何转开发a why it isn't working. I have the following controller action

public JsonResult GetSubCategory(int category) {
            //var subCat = db.categories.Where(c => c.parentID == category).Select(c => new {c.ID, c.codeDesc}).ToList();
            var subCat = from c in db.categories
                         where c.parentID == category
                         select new
                         {
                             ID = c.ID,
                             codeDesc = c.codeDesc
                         };

            return Json(subCat); 
}

I have the following jquery method in my View file

$("#catList").change(function () {
    alert("In the change event. cat val is " + $("#catList").val());
    var category = $("#catList").val()
    $.getJSON("/Issues/Search/GetSubCategory", { 'category': category },
        function (data) {
            alert("in the func"); 
            $("#subCatList").options.length = 0; 
            $(data).each(function () {
                $("<option>").val(this.ID).text(this.codeDesc).appendTo("#subCatList");
            });
        });
});

At the very least I just want the alert message to be displayed but it doesn't seem to be displayed. I've tried both ways of building the subCat object as you can see in the controller code above. And the debug shows me that data is being generated with ID and codeDesc fields and values, but nothing is happening on the View side. Please can somebody help in figuring out what I'm doing wrong?

Thanks


If the alert is not happening, it means the JSON call is failing. Try adding the following after the end of the JSON call:

.error(function() {
    alert("JSON call failed");
});

If you get that alert, the next step is to determine why the JSON call isn't working.


Probably should add a semi-colon at the end of line 3 in your View file.


ANy action result that returns Json data in response to a get request must have:

    JsonRequestBehavior.AllowGet

In it's return statement. This is deliberate in the MS implementation to prevent Json related attacks by posting spurious data via get to an action result.

so in your case:

    return Json(subCat,JsonRequestBehavior.AllowGet);

Will solve your problem.

On a side note, another thing that will cause this is if your not returning the correct mime type, but this generally only applies if your hand coding your result handlers instead of using MVC.

What I mean by this is, if say your geared up to return HTML (and the server returns "Content-type: text/html") then your Ajax request looking for Json will fail silently in the same way, unless you set the response type to "application/json"


As other suggeste maybe the problem lies in the fact that you have an error in the col. You could try using

$.ajax({
  url: "/Issues/Search/GetSubCategory",
  data: { 'category': category },
  success:  function (data) {
            alert("in the func"); 
            $("#subCatList").options.length = 0; 
            $(data).each(function () {
                $("<option>").val(this.ID).text(this.codeDesc).appendTo("#subCatList");
            });,
  error: function(){
          alert('error');
        },
  dataType: "json"
});

if you see "error" the problem is that the call is not successful


Thank you for your helpful responses. As I stated in my comment above, this was because I wasn't calling it with the POST option.
Also as Jonathon and Shawty rightly stated, you could also indicate that you want a GET to return results, but from what I read this wasn't considered safe. (I'd like to hear your comments if you don't think this is true of course).

Thank you

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜