weixin_33705053 2013-03-26 07:57 采纳率: 0%
浏览 249

JSON Stringify空字符串

I've recently ran into a problem with JSON.stringify not taking an empty string.

I solved it with a small fix but i'm still bothered why it doesn't work with an empty string

Here is my code.

$(function () {
    $('#searchButton').click(function () {
        var test = $("#DownloadsSearch").val();
        alert(test);
        if (test === "") test = "1";

        displayAjaxLoading(true);
        $.ajax({
            url: '@(Url.RouteUrl("DownloadSearch"))',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify({
                searchText: test,
                type: '@Model.Type'
            }),
            type: 'POST',
            success: function (data) {
                if (data.length > 0) {
                    $("#Review").empty();

                    $.each(data, function (id, options) {
                        $("<p/>").appendTo("#Review").append($('<a>').attr('href', 'http://www.pvs4you.com/' + options.URL).text(options.ProductID));

                        // $('<a>').attr('href', options.URL).text(options.ProductID).appendTo("#Review");
                    });
                } else {
                    $("#Review").empty();
                    $("<p/>").html("<b>@Model.Title wasn't found.</b>").appendTo($("#Review"));
                }

                $('#small-searchterms').autocomplete({
                    disabled: false
                });
                displayAjaxLoading(false);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $("#Review").empty();
                $("<p/>").html("<b>@Model.Title wasn't found.</b>").appendTo($("#Review"));
            }
        });

    });
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元