【发布时间】:2014-02-14 09:38:16
【问题描述】:
$('#deviceLoadMore') 是一个链接。单击此链接时,它将触发我创建的 Web 服务的 ajax。
我现在遇到的问题是它不断在 console.log 中抛出这个错误 未捕获的 TypeError:将循环结构转换为 JSON。但是当我只是将 ajax 部分粘贴到 console.log 中时,它能够检索回数据。我检查过所有的值都只是一个普通的字符串和整数。
我想知道为什么我可以在控制台日志中触发而没有任何问题,而如果我只是点击链接就不能?
var currentContextSection = '<%=currentSection %>';
var currentTemplateIds = '<%=templateIds %>';
var currentItemPerPage = <%=itemPerPage %>;
var currentPageIndex = <%=currentPage %>;
var arguments = { templateIds:'<%=templateIds %>' , currentSection:'<%=templateIds %>', currentPage:currentPageIndex, itemPerPage:currentItemPerPage };
$(document).ready(function () {
$('#deviceLoadMore').click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/AJAX/WS.asmx/GetItems",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(arguments),
dataProcess: false
}).done(function (data) {
test = data;
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
});
});
-------- 编辑 -------- 如果我有这个:
var arguments = {"templateIds":currentTemplateIds ,"currentSection":currentContextSection,"currentPage":currentPageIndex,"itemPerPage":currentItemPerPage};
并使用 ajax 数据执行:JSON.stringify(arguments),我会得到以下错误: 将循环结构转换为 JSON。 当我控制台记录“参数”时,它会显示:
Object {templateIds: "963C1D18A93849309D6014CE2135173C", currentSection: "Personal", currentPage: 1, itemPerPage: 8}
当我 console.log JSON.stringify(arguments) 时它会显示这个:
"{"templateIds":"963C1D18A93849309D6014CE2135173C","currentSection":"Personal","currentPage":1,"itemPerPage":8}"
在谷歌搜索了一些成功实现的 ajax 示例后,我将代码更改为以下内容,并且可以正常工作!而且我不知道为什么会这样。
$(document).ready(function () {
$('#deviceLoadMore').click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/AJAX/WS.asmx/GetItems",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({"templateIds":currentTemplateIds ,"currentSection":currentContextSection,"currentPage":currentPageIndex,"itemPerPage":currentItemPerPage}),
dataProcess: false
}).done(function (data) {
test = data;
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
});
});
【问题讨论】:
-
您是否尝试过将参数初始化放入 document.ready 中?
-
Uncaught TypeError: Converting circular structure to JSON当您尝试像对象的属性一样使用同一对象或 this 的引用时,会触发此错误。像obj = {}, obj.a = a或a = {}; b = a, a.a = b,在这两种情况下你不能使用 JSON.stringify() 因为 a.a 是它自己的引用。 -
如果你在参数初始化之后执行 console.log(arguments) 会在控制台中追加什么
标签: javascript jquery .net ajax