views:

199

answers:

2

I am trying to POST data back to the server and get a dataset back. What I am getting back instead is the HTML for the whole page AND it is not kicking off the Action in my Controller. And yes - JQuery is still confusing to me.

The code I currently have is:

function updateNavIndex(pageIndex) {
    var filters = $("form").serialize();
    var productGroup = $("#valProductGroup").attr('title');
    var productType = $("#valProductType").attr('title');
    var itemsPerPage = $("#ItemsPerPage").val();

    $.ajax({ path: "/CatalogAjaxController/UpdateNavigation"
          , type: "POST"
          , data: "{ productGroup: " + productGroup + ", productType: " + productType + ", itemsPerPage: " + itemsPerPage + ", pageIndex: " + pageIndex + ", filters: " + filters + "}"
          , success: function(data) { handleMenuData(data); }
    });
    $.ajax({ path: "/CatalogAjaxController/UpdateProducts"
          , type: "POST"
          , data: "{ productGroup: " + productGroup + ", productType: " + productType + ", itemsPerPage: " + itemsPerPage + ", pageIndex: " + pageIndex + ", filters: " + filters + "}"
          , success: function(data) { handleProductData(data); }
    });
}

The callback function is tripping and the function definition looks like:

function handleMenuData(data) {
    $("#navigation ul").remove();
    }

At this point the "data" variable has the Page HTML in it. (And like I said, the Action did not fire off.) I am thinking that I should not be calling the $ajax function directly but using a var function definition. Something like:

var retrieveMenuData = function(path, productGroup, productType, itemsPerPage, pageIndex, filter, fnHandleCallback) {
    $.ajax( path
          , type
          , { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage, pageIndex: pageIndex, filter: filter }
          , function(data) { fnHandleCallback(data); });
};

I'm not sure if I have it defined correctly or how to call it correctly. ANy help is appreciated!

+3  A: 

The solution may be pretty simple.

You're using path: where it should have url:

Source: jQuery.ajax options list

R. Bemrose
+2  A: 

also, you've got some odd quotes in there.. the syntax for data: is more like this:

$.ajax({
    type: "POST",
    url: "bin/login.php",
    data:{data1: var1,data2: var2},
    success: function(data) {
        }
     });
Ryan Rohrer
Ah, good point... it doesn't make a lot of sense to quote JSON in javascript... JSON is Javscript Object Notation, after all.
R. Bemrose
I am using the post at "http://stackoverflow.com/questions/799404/ajax-and-mvc-c" and I am still getting the error "data is undefined" - where data is supposed to be the return from the Controller.Action method.
Keith Barrows
My var function definition lookls like: var retrieveData2 = function(path, productGroup, productType, itemsPerPage, pageIndex, filters, fnHandleCallback) {$.getJSON(path, { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage, pageIndex: pageIndex, filter: filter }, function(data) { fnHandleCallback(data); });}; When I call it is when I get the error. My call looks like: retrieveData2("/CatalogAjaxController/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData(data));*confused*
Keith Barrows
I always thought the contentType attribute was mandatory! Is it mandatory because I don't see you using in the above code!
azamsharp
No, contentType defaults to application/x-www-form-urlencoded (the encoding for a GET/POST query string).
R. Bemrose