[英]Pass a variable to Ajax URL
I'm trying to add a variable (integer) in URL to be used in an Ajax request: 我试图在URL中添加一个变量(整数)以在Ajax请求中使用:
In my HTML, I have: 在我的HTML中,我有:
<span class="user">15</span>
And the script: 和脚本:
$('#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);
}
});
});
}
});
});
However, the variable (15) isn't being passed to the URL, I get the error: 但是,变量(15)没有传递到URL,我得到了错误:
/files/NaN/data.json
I know I'm doing something stupid, and searched everywhere, but what is it? 我知道我在做一些愚蠢的事,到处搜寻,但这是什么?
You don't really have to parse int it, because you're just putting it back into a string. 您实际上不必解析int,因为您只是将其放回字符串中。 Try removing the parse int then consol logging the value of the user variable. 尝试删除parse int,然后合并记录用户变量的值。
Also, perhaps you have many user elements? 另外,也许您有许多用户元素? Console log $(".user") to see if you get an array. 控制台日志$(“。user”)来查看是否有数组。
It looks like the parsing fails and you get NaN
(not-a-number) instead. 看起来解析失败,而您得到的是NaN
(非数字)。
Can you verify that the input string to parseInt
looks like a number? 您可以验证要parseInt
的输入字符串看起来像数字吗?
Also, you're converting user
back into a string. 另外,您正在将user
转换回字符串。 Do you really need to parseInt
here? 您真的需要在这里parseInt
吗?
I suggest you try to run $('.user').text()
in chrome console (F12) (for example) to see if you have some empty value or thing like that. 我建议您尝试在chrome控制台(F12)中运行$('.user').text()
)(例如),看看您是否有一些空值或类似的值。
I believe that this approach can help you. 我相信这种方法可以为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.