我的ajax请求成功返回,但数据似乎不存在。我知道json序列化有效,因为如果我对数据库进行查询并对其进行序列化,则会正确返回查询结果。在下面的例子中,我得到的只是“[]”。
编辑:我还做过其他测试,例如尝试从itemsInCart
中提取单个数据,并且它似乎完全为空(这证明了我得到的响应)
型号:
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; }
}
控制器:
[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);
}
});
请求URL(来自开发人员工具):
http://localhost:52459/Sale/completeSale?itemsInCart=[{"ItemId":1,"Quantity":"1","Price":3.5}]
答案 0 :(得分:0)
首先,您不要向GET请求提交数据。
其次,试试这个
$.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);
}
});
指定在浏览器中打开的完整路径