0

My ajax request is returning with success, but the data doesn't seem to be there. I know the json serialization works because if I do a query on the database and serialize that, the query results are properly returned. In the case below, all I get back is "[]".

Edit: I've also done other tests like try to extract a single piece of data from itemsInCart, and it appears to be totally empty (which justifies the response I get).

Model:

public class ItemInCart
{
    [Key]
    public int ItemId { get; set; }
    public virtual Variety variety { get; set; }

    public int Quantity { get; set; }
    public virtual InventoryItem inventoryItem { get; set; }

    public double Price { get; set; }
    public virtual Variety price { get; set; }

}

Controller:

    [HttpGet]
    public ActionResult completeSale(List<ItemInCart> itemsInCart)
    {
        var json = new JavaScriptSerializer().Serialize(itemsInCart);
        return Json(json, JsonRequestBehavior.AllowGet);
    }

Ajax:

$.ajax({
    type: "GET",
    url: "/" + current_controller + "/completeSale", // the method we are calling
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { "itemsInCart": itemsInCart },
    success: function (result) {
        alert("success " + JSON.stringify(result));
    },
    error: function (result) {
        alert("failed " + result);
    }

});

Request URL (from developer tools):

http://localhost:52459/Sale/completeSale?itemsInCart=[{"ItemId":1,"Quantity":"1","Price":3.5}]
abalter
  • 9,663
  • 17
  • 90
  • 145
  • did you tried `alert("success " + result.quantity);` ? – Adib Aroui Jun 29 '15 at 20:20
  • your content type doesn't match the data you are sending. `{ "itemsInCart": itemsInCart }` is an object, not json. i suggest removing the contentType option. – Kevin B Jun 29 '15 at 20:36
  • `result.quantity` => `undefined` – abalter Jun 29 '15 at 20:39
  • @KevinB But it is `JSON.stringify`'d – abalter Jun 29 '15 at 20:40
  • it's still an object, not json. it's an object, that contains a string that just so happens to be json. it then gets converted into a param string, rather than being sent as json in the request body (which you can see by the request url) – Kevin B Jun 29 '15 at 20:41
  • Hmmm. I've seen lots of solutions on SO doing exactly that. I tried removing contentType and same result. What do you think of the Request URL posted above? – abalter Jun 29 '15 at 20:42
  • The request url is fine, assuming the controller knows how to consume it. I don't know ASP, so i can't help past that. – Kevin B Jun 29 '15 at 20:43
  • BTW, for instance, I tried the same thing as [this](http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax) it did not work for me. – abalter Jun 29 '15 at 20:45
  • That is an example of the contentType used properly. Note how the stringified object is passed directly to `data:` rather than passing an object that contains the stringified object. it's also kinda weird to be passing json along with a GET request in my opinion. – Kevin B Jun 29 '15 at 20:45

1 Answers1

0

First, you do not submit data to GET request.

Second, try this

   $.ajax({
        type: "GET",
        url: "http://localhost:59945/wmain/completesale", // the method we are calling
        contentType: "application/json; charset=utf-8",

        dataType: "json",
        success: function (result) {
            var v = JSON.parse(result);
            alert(v.name);
            alert('Yay! It worked!tim' + result);

            // Or if you are returning something

        },
        error: function (result) {
            alert('Oh no aa>>>> :(' + result.responseText);
        }
    });

Specify complete path that opens up in your browser

user786
  • 3,902
  • 4
  • 40
  • 72
  • Yes, I should POST, but in POST you can't see what you are actually sending, so it's harder to debug. Yes, that works. And, indeed, I'm having fine success with sending single objects. But a list of objects is not working. – abalter Jun 29 '15 at 21:10