我有控制器功能返回:
return Json(new { model = testVM, html = RenderViewToString(ControllerContext, "_test", testVM) }, JsonRequestBehavior.AllowGet);
("型号"由参数组成," html"是带有html和数据的局部视图)
如果我像这样使用经典的jQuery ajax,一切都运行良好:
$.ajax({
url: actionUrl,
type: 'POST',
dataType: 'json',
cache: false,
async: true,
success: function (data) {
console.log(data); //{model:{id:1, name: "aa", ...},html:"<span>test</span>"}
console.log(data.model.id); //I can get ID from model
console.log(data.html); //I can render this into div
}
});
..但是当我尝试切换到ES6 fetch()时,我收到错误(使用Chrome调试器):
error1: Uncaught (in promise) TypeError: a.replace is not a function
或
error2: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
我的获取代码:
fetch(url, {
method: "POST",
mode: "cors",
headers: new Headers({
"Content-Type": "json"
}),
})
.then(function(response){ return response.json() })
.then(fucntion(j){ console.log(j) })
.catch()
所以问题是如何访问&#34; model&#34;和&#34; html&#34; ?