使用jQuery Ajax时出错

时间:2014-02-14 09:38:17

标签: javascript jquery .net ajax

$('#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。 当我在console.log中显示“参数”时,它会显示:

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);
        });
    });
});

1 个答案:

答案 0 :(得分:0)

中删除 JSON.stringify 后尝试
 data: JSON.stringify(arguments),