试图从MVC控制器获取JSON并获取错误网:: ERR_INCOMPLETE_CHUNKED_ENCODING
我试图从我的控制器获取JSON以在jQuery中使用。 我有以下代码。 当我在浏览器中访问URL时,它返回json,所以我知道控制器正在工作……但我得到以下内容
GET http:// localhost:52802 / Checkout / GetContactById?id = 1 net :: ERR_INCOMPLETE_CHUNKED_ENCODING
客户端jQuery
var theUrl = window.location.origin + '/Checkout/GetContactById?id=' + contactId; $.ajax({ url: theUrl, type: "GET", success: function (result) { alert("Success"); }, error: function (error) { alert("Error"); } });
服务器端控制器
[HttpGet] public IActionResult GetContactById(int id) { Contact contact = this.checkoutDataAccess.GetContactById(id); return Json(contact); }
我得到的唯一提醒是“错误”
更新
经过更多的研究,看起来一切都在加载……但我收到了一个错误
无法加载响应数据
在控制台中。
我逐步完成了代码,一切都在控制器端工作。 我不确定它是不是格式化JSON正确,或者我没有正确接收它。 无论哪种方式,我都不知道发生了什么。
实际上,因为你输入警报(“错误”),你得到文本错误; 尝试提醒(错误); 相反或尝试使用firebug进行调试
尝试将数据类型JSON添加到ajax调用。
也许尝试在正文中添加参数。
像这样:
var theUrl = window.location.origin + '/Checkout/GetContactById'; $.ajax({ url: theUrl, type: "GET", dataType: "json", data: { id: contactId }, success: function (result) { alert("Success"); }, error: function (error) { alert("Error"); } });
我建议使用Url.Action助手。
喜欢:
$.ajax({ url: '@Url.Action("GetContactById", "Checkout")', type: "GET", dataType: "json", data: { id: contactId }, success: function (result) { alert("Success"); }, error: function (error) { alert("Error"); } });