【发布时间】:2021-08-17 11:37:15
【问题描述】:
function UserCheckId() {
$.ajax({
type: "POST",
dataType: "json",
url: "/Home/SomeAction",
data: { qrcode: scannedQR[txt] },
dataType: 'json',
success: function (data) {
if (data = "Storekeeper") {
document.location.replace("/Storekeeper.aspx");
}
else {
alert("Error");
}
}
});
有一个 UserCheckId 函数,我在其中从 C# 调用 SomeAction 函数(它返回一个字符串值)并将结果传递给 Javascript。之后,我想检查结果的值是多少。如果是“Storekeeper”,则转到该站点,否则会弹出错误。问题是无论值是什么(例如,C# 将返回值“Collector”),Storekeeper 的条件在任何情况下都满足。我用警报检查了数据,它正确输出了字符串值。该怎么办?请帮帮我!
【问题讨论】:
-
我不明白这一行
. The problem is that whatever the value is (for example, C # will return the value "Collector"), the condition for the Storekeeper is met in any case. -
=是一个作业。用于比较使用==或===。 assinment 将始终返回分配的值,因为这是非空字符串,在if的条件内,它将评估为真值,因此将执行 if 的第一个分支。 -
做
console.log(data)看看它打印了什么。 -
@Swati 如果你把它放在
if之前,它将打印data在if之前的任何值。如果你把它放在if之后,它会在if之后打印Storekeeper,因为这是data在条件if(data = "Storekeeper")中分配的值
标签: javascript c# asp.net json ajax