0

我正在尝试在要在 Ajax 请求中使用的 URL 中添加一个变量(整数):

在我的 HTML 中,我有:

<span class="user">15</span>

和脚本:

$('#fileupload').click(function () {
    $(this).fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                if (file.error != null) {
                    $console.text(file.error);
                }
                var user = parseInt($(".user").text(), 10);
                //Open the uploaded file//
                $.ajax({
                    url: "files/" + user + '/' + file.name,
                    dataType: 'json',
                    type: 'GET',
                    success: function (res) {
                        handsontable.loadData(res.data);
                        $console.text('Loaded file: ' + file.name);
                    }
                });
            });
        }
    });
});

但是,变量 (15) 没有传递给 URL,我收到错误:

/files/NaN/data.json

我知道我在做一些愚蠢的事情,并且到处搜索,但它是什么?

4

3 回答 3

1

您实际上不必解析 int 它,因为您只是将其放回字符串中。尝试删除 parse int 然后 consol 记录用户变量的值。

另外,也许你有很多用户元素?控制台记录 $(".user") 以查看是否获得了数组。

于 2013-10-10T16:51:07.960 回答
1

看起来解析失败了,你得到NaN了 (not-a-number) 。

你能验证输入字符串parseInt看起来像一个数字吗?

此外,您正在转换user回字符串。你真的需要在parseInt这里吗?

于 2013-10-10T16:49:36.660 回答
0

我建议您尝试$('.user').text()在 chrome 控制台 (F12) 中运行(例如),看看您是否有一些空值或类似的东西。

我相信这种方法可以帮助你。

于 2013-10-10T17:03:10.513 回答